diff --git a/src/generators/model/Generator.php b/src/generators/model/Generator.php index 86abe3522..1c7684a4b 100644 --- a/src/generators/model/Generator.php +++ b/src/generators/model/Generator.php @@ -310,9 +310,12 @@ public function generate() 'relationsClassHints' => $this->generateRelationsClassHints($tableRelations, $this->generateQuery), 'enum' => $this->getEnum($tableSchema->columns), ]; + + $template = phpversion() < '7' ? 'model.php': 'model_from_7.php'; + $files[] = new CodeFile( Yii::getAlias('@' . str_replace('\\', '/', $this->ns)) . '/' . $modelClassName . '.php', - $this->render('model.php', $params) + $this->render($template, $params) ); // query: diff --git a/src/generators/model/default/model_from_7.php b/src/generators/model/default/model_from_7.php new file mode 100644 index 000000000..feb2eda90 --- /dev/null +++ b/src/generators/model/default/model_from_7.php @@ -0,0 +1,165 @@ + [type, name. comment]) */ +/** @var string[] $labels list of attribute labels (name => label) */ +/** @var string[] $rules list of validation rules */ +/** @var array $relations list of relations (name => relation declaration) */ + +echo " + +namespace ns ?>; + +use \yii\db\Connection; +/** + * This is the model class for table "generateTableName($tableName) ?>". + * + $data): ?> + * @property ' ']) : '') . "\n" ?> + + + * + $relation): ?> + * @property + + + */ +class extends baseClass, '\\') . "\n" ?> +{ + + + /** + * ENUM field values + */ + $columnData) { + foreach ($columnData['values'] as $enumValue){ + echo ' const ' . $enumValue['constName'] . ' = \'' . $enumValue['value'] . '\';' . PHP_EOL; + } + } +endif +?> + + /** + * {@inheritdoc} + */ + public static function tableName() : string + { + return 'generateTableName($tableName) ?>'; + } +db !== 'db'): ?> + + /** + * @return Connection the database connection used by this AR class. + */ + public static function getDb() : Connection + { + return Yii::$app->get('db ?>'); + } + + + /** + * {@inheritdoc} + */ + public function rules() : array + { + return []; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() : array + { + return [ + $label): ?> + " . $generator->generateString($label) . ",\n" ?> + + ]; + } + $relation): ?> + + /** + * Gets query for [[]]. + * + * @return + */ + public function get() : + { + + } + + +ns === $generator->queryNs) ? $queryClassName : '\\' . $generator->queryNs . '\\' . $queryClassName; + echo "\n"; +?> + /** + * {@inheritdoc} + * @return the active query used by this AR class. + */ + public static function find() : + { + return new (get_called_class()); + } + + + + $columnData): ?> + + /** + * column ENUM value labels + * @return string[] + */ + public static function () : array + { + return [ + $value): ?> +enableI18N) { + echo ' self::' . $value['constName'] . ' => Yii::t(\'' . $generator->messageCategory . '\', \'' . $value['value'] . "'),\n"; + } else { + echo ' self::' . $value['constName'] . ' => \'' . $value['value'] . "',\n"; + } + ?> + + ]; + } + + $columnData): ?> + + /** + * @return string + */ + public function () : string + { + return self::()[$this->]; + } + + + /** + * @return bool + */ + public function () : bool + { + return $this-> === self::; + } + + public function () + { + $this-> = self::; + } + + + +} diff --git a/tests/generators/ModelGeneratorTest.php b/tests/generators/ModelGeneratorTest.php index bb1d9f6d4..6ce572244 100644 --- a/tests/generators/ModelGeneratorTest.php +++ b/tests/generators/ModelGeneratorTest.php @@ -506,7 +506,9 @@ public function testEnum() 'relationsClassHints' => [], 'enum' => $generator->getEnum($tableSchema->columns), ]; - $codeFile = $generator->render('model.php', $params); + + $template = phpversion() < '7' ? 'model.php': 'model_from_7.php'; + $codeFile = $generator->render($template, $params); /** * Fix class code for eval - remove ?php, namespace and use Yii