本文整理汇总了PHP中yii\db\ActiveRecord::getTableSchema方法的典型用法代码示例。如果您正苦于以下问题:PHP ActiveRecord::getTableSchema方法的具体用法?PHP ActiveRecord::getTableSchema怎么用?PHP ActiveRecord::getTableSchema使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\db\ActiveRecord
的用法示例。
在下文中一共展示了ActiveRecord::getTableSchema方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getTableSchema
public static function getTableSchema()
{
$class = get_called_class();
if (!isset(self::$getTableSchemaCache[$class])) {
self::$getTableSchemaCache[$class] = parent::getTableSchema();
}
return self::$getTableSchemaCache[$class];
}
示例2: init
/**
* @throws \jobbyDb\exception\WrongSchemaException If not all required columns are present in the table
*/
public function init()
{
parent::init();
// check if all the column names needed are present
$schema = parent::getTableSchema();
$columnsPresent = $schema->getColumnNames();
foreach (static::$columnsRequired as $required) {
if (!in_array($required, $columnsPresent)) {
throw new WrongSchemaException("No {$required} column found in '{$this->getTableSchema()}''");
}
}
}
示例3: getRelationRoutes
/**
* @param \yii\db\ActiveRecord $model
* @param \yii\db\ActiveRecord $relatedModel
* @param \yii\db\ActiveQuery $relation
* @return array array with three arrays: create, search and index routes
*/
public static function getRelationRoutes($model, $relatedModel, $relation)
{
if (($route = Yii::$app->crudModelsMap[$relatedModel::className()]) === null) {
return [null, null, null];
}
$allowCreate = Yii::$app->user->can($relatedModel::className() . '.create');
if ($allowCreate && $model->isNewRecord && $relation->multiple) {
foreach ($relation->link as $left => $right) {
if (!$relatedModel->getTableSchema()->getColumn($left)->allowNull) {
$allowCreate = false;
break;
}
}
}
if (!$allowCreate) {
$createRoute = null;
} else {
$createRoute = [$route . '/update'];
if ($relation->multiple) {
$createRoute['hide'] = implode(',', array_keys($relation->link));
$scope = $relatedModel->formName();
$primaryKey = $model->getPrimaryKey(true);
foreach ($relation->link as $left => $right) {
if (!isset($primaryKey[$right])) {
continue;
}
$createRoute[$scope][$left] = $primaryKey[$right];
}
}
}
$parts = explode('\\', $relatedModel::className());
$relatedModelClass = array_pop($parts);
$relatedSearchModelClass = implode('\\', $parts) . '\\search\\' . $relatedModelClass;
$searchRoute = !class_exists($relatedSearchModelClass) ? null : [$route . '/relation', 'per-page' => 10, 'relation' => $relation->inverseOf, 'id' => Action::exportKey($model->getPrimaryKey()), 'multiple' => $relation->multiple ? 'true' : 'false'];
$indexRoute = [$route . '/index'];
return [$createRoute, $searchRoute, $indexRoute];
}
示例4: getFormElements
/**
* Format same as kartik\builder\Form::$attributes
* @param ActiveRecord $model
* @return array
*
* @see kartik\builder\Form::$attributes
*/
public function getFormElements($model)
{
$res = [];
$attributes = $model->getAttributes();
foreach ($model->getTableSchema()->primaryKey as $pk) {
unset($attributes[$pk]);
}
$attributes = array_keys($attributes);
foreach ($attributes as $attribute) {
$res[$attribute]['type'] = Form::INPUT_TEXT;
}
$res[] = $this->getActionRow($model);
return $res;
}
示例5: getAttributeWidget
/**
* Get attributes widget.
*
* @param \yii\db\ActiveRecord $model Model
* @param string $attribute Model attribute
* @return null|string|object
*/
public function getAttributeWidget($model, $attribute)
{
if ($this->attributeWidgets !== null) {
if (isset($this->attributeWidgets->{$attribute})) {
return $this->attributeWidgets->{$attribute};
} else {
$tableSchema = $model->getTableSchema();
$column = $tableSchema->columns[$attribute];
if ($column->phpType === 'boolean') {
return 'checkbox';
} elseif ($column->type === 'text') {
return 'textarea';
} elseif (preg_match('/^(password|pass|passwd|passcode)$/i', $column->name)) {
return 'password';
} else {
return 'text';
}
}
}
$attributeWidgets = [];
if (method_exists($model, 'attributeWidgets')) {
$attributeWidgets = $model->attributeWidgets();
}
$data = [];
if (!empty($attributeWidgets)) {
foreach ($attributeWidgets as $item) {
if (isset($item[0]) && isset($item[1])) {
$data[$item[0]] = $item[1];
$data[$item[0] . 'Options'] = $item;
}
}
}
$this->attributeWidgets = (object) $data;
return $this->getAttributeWidget($model, $attribute);
}
示例6: getPkColumnName
/**
* Get PK column name
* @param ActiveRecord $model
* @return mixed
*/
public static function getPkColumnName(ActiveRecord $model)
{
return $model->getTableSchema()->primaryKey[0];
}
示例7: excludeField
/**
* @param ActiveRecord $model
*/
protected function excludeField($model, $addition = '')
{
$this->fieldExclude = ArrayHelper::merge(is_array($this->excludedField) ? $this->excludedField : [$this->excludedField], $this->fieldExclude);
if (is_callable($this->excludeFieldCallback)) {
$func = $this->excludeFieldCallback;
$result = $func($this);
$result = $result ? is_array($result) ? $result : [$result] : [];
$this->fieldExclude = ArrayHelper::merge($this->fieldExclude, $result);
}
$this->fieldExclude = ArrayHelper::merge($this->fieldExclude, $model->getTableSchema()->primaryKey);
$behaviors = $model->getBehaviors();
foreach ($behaviors as $_next) {
$_nextCopy = $_next;
$_next = (array) $_next;
$_next['class'] = $_nextCopy->className();
if ($_next['class'] === BlameableBehavior::className()) {
if (isset($_next['updatedByAttribute'])) {
if (!empty($_next['updatedByAttribute'])) {
$this->fieldExclude[] = $addition . $_next['updatedByAttribute'];
}
} else {
$this->fieldExclude[] = $addition . 'updated_by';
}
if (isset($_next['createdByAttribute'])) {
if (!empty($_next['createdByAttribute'])) {
$this->fieldExclude[] = $addition . $_next['createdByAttribute'];
}
} else {
$this->fieldExclude[] = $addition . 'created_by';
}
} else {
if ($_next['class'] === TimestampBehavior::className()) {
if (isset($_next['updatedAtAttribute'])) {
if (!empty($_next['updatedAtAttribute'])) {
$this->fieldExclude[] = $addition . $_next['updatedAtAttribute'];
}
} else {
$this->fieldExclude[] = $addition . 'updated_at';
}
if (isset($_next['createdAtAttribute'])) {
if (!empty($_next['createdAtAttribute'])) {
$this->fieldExclude[] = $addition . $_next['createdAtAttribute'];
}
} else {
$this->fieldExclude[] = $addition . 'created_at';
}
} else {
if ($_next['class'] === PhoneInputBehavior::className()) {
if (!$this->searchColumn($_next['phoneAttribute'])) {
$this->columns[] = new WidgetsCrud(['fieldName' => $_next['phoneAttribute'], 'widgetType' => 'phone']);
}
} else {
if ($_next['class'] === UploadBehavior::className()) {
if (!is_array($this->hasUploadBehavior)) {
$this->hasUploadBehavior = ['attributes' => []];
}
$this->hasUploadBehavior['attributes'][] = isset($_next['attribute']) ? $_next['attribute'] : $_next['attribute'];
$this->hasUploadBehavior[] = $_next['pathAttribute'];
$this->hasUploadBehavior[] = $_next['baseUrlAttribute'];
$this->fieldExclude[] = $addition . $_next['pathAttribute'];
$this->fieldExclude[] = $addition . $_next['baseUrlAttribute'];
if (!$this->searchColumn($this->hasUploadBehavior)) {
$this->columns[] = new WidgetsCrud(['fieldName' => $addition . $_next['attribute'], 'widgetType' => 'upload']);
}
if (isset($this->columnUsed['upload']) && ($uses = $this->columnUsed['upload'])) {
if (isset($uses['expression'])) {
$this->expressions = ArrayHelper::merge($this->expressions, $uses['expression']);
unset($this->columnUsed['expression']);
}
}
$this->used = ArrayHelper::merge($this->used, $uses);
} else {
if ($_next['class'] === 'omgdef\\multilingual\\MultilingualBehavior') {
$this->isMultilingual = true;
$this->languageField = $_next['languageField'];
$this->translateAttribute = $_next['attributes'];
$this->relationField = $_next['langForeignKey'];
$class = $_next['langClassName'] . ($_next['dynamicLangClass'] ? isset($_next['langClassSuffix ']) ? $_next['langClassSuffix '] : '' : '');
$this->relationClass = $class;
if (class_exists($class)) {
/** @var ActiveRecord $class */
$class = new $class();
$this->translateTable = $class::tableName();
} else {
$this->translateTable = $_next['tableName'];
}
$this->excludeField($class, 'translations.');
}
}
}
}
}
}
$this->columnUsed = ArrayHelper::merge($this->columnUsed, $this->widgetsUseClass);
}