From b08de00557c1a8a3e3ebdbe10823847438c5b927 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Thu, 31 Aug 2023 14:27:37 +0300 Subject: [PATCH] Add properties --- src/Generator/ActiveRecord/Generator.php | 29 +++++++++++++++++++- src/Generator/ActiveRecord/default/model.php | 22 +++++++++++---- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/Generator/ActiveRecord/Generator.php b/src/Generator/ActiveRecord/Generator.php index fe4b4c24..f7e79b77 100644 --- a/src/Generator/ActiveRecord/Generator.php +++ b/src/Generator/ActiveRecord/Generator.php @@ -5,15 +5,28 @@ namespace Yiisoft\Yii\Gii\Generator\ActiveRecord; use InvalidArgumentException; +use Yiisoft\Aliases\Aliases; +use Yiisoft\Db\Connection\ConnectionInterface; +use Yiisoft\Validator\ValidatorInterface; use Yiisoft\Yii\Gii\Component\CodeFile\CodeFile; use Yiisoft\Yii\Gii\Generator\AbstractGenerator; use Yiisoft\Yii\Gii\GeneratorCommandInterface; +use Yiisoft\Yii\Gii\ParametersProvider; /** * This generator will generate a controller and one or a few action view files. */ final class Generator extends AbstractGenerator { + public function __construct( + Aliases $aliases, + ValidatorInterface $validator, + ParametersProvider $parametersProvider, + private ConnectionInterface $connection, + ) { + parent::__construct($aliases, $validator, $parametersProvider); + } + public static function getId(): string { return 'active-record'; @@ -46,10 +59,24 @@ public function doGenerate(GeneratorCommandInterface $command): array $rootPath = $this->aliases->get('@root'); + $properties = []; + if ($schema = $this->connection->getTableSchema($command->getTableName(), true)){ + foreach ($schema->getColumns() as $columnSchema) { + $properties[] = [ + 'name' => $columnSchema->getName(), + 'type' => match ($columnSchema->getPhpType()) { + 'integer' => 'int', + default => 'string', + }, + 'isAllowNull' => $columnSchema->isAllowNull(), + 'defaultValue' => $columnSchema->getDefaultValue(), + ]; + } + } $path = $this->getControllerFile($command); $codeFile = (new CodeFile( $path, - $this->render($command, 'model.php') + $this->render($command, 'model.php', ['properties' => $properties]) ))->withBasePath($rootPath); $files[$codeFile->getId()] = $codeFile; diff --git a/src/Generator/ActiveRecord/default/model.php b/src/Generator/ActiveRecord/default/model.php index f71c68ee..6eff74b7 100644 --- a/src/Generator/ActiveRecord/default/model.php +++ b/src/Generator/ActiveRecord/default/model.php @@ -1,13 +1,14 @@ $properties + */ + echo " @@ -19,6 +20,17 @@ final class getModelName(); ?> extends getBaseClass()) . PHP_EOL ?> { + + private ; + + public function tableName(): string { return 'getTableName() ?>';