getTableSchema() public static method

Returns the schema information of the DB table associated with this AR class.
public static getTableSchema ( ) : yii\db\TableSchema
return yii\db\TableSchema the schema information of the DB table associated with this AR class.
コード例 #1
0
 public static function getTableSchema()
 {
     $class = get_called_class();
     if (!isset(self::$getTableSchemaCache[$class])) {
         self::$getTableSchemaCache[$class] = parent::getTableSchema();
     }
     return self::$getTableSchemaCache[$class];
 }
コード例 #2
0
 /**
  * @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
0
ファイル: FormBuilder.php プロジェクト: netis-pl/yii2-crud
 /**
  * @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
0
ファイル: MAdminController.php プロジェクト: e96/yii2-madmin
 /**
  * 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
0
ファイル: Module.php プロジェクト: janisto/yii2-ycm
 /**
  * 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
0
ファイル: ModelHelper.php プロジェクト: vladdnepr/yii2-ycm
 /**
  * Get PK column name
  * @param ActiveRecord $model
  * @return mixed
  */
 public static function getPkColumnName(ActiveRecord $model)
 {
     return $model->getTableSchema()->primaryKey[0];
 }
コード例 #7
0
 /**
  * @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);
 }