Skip to content

Commit

Permalink
Add properties
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Aug 31, 2023
1 parent 9b0b4e4 commit b08de00
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
29 changes: 28 additions & 1 deletion src/Generator/ActiveRecord/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;

Expand Down
22 changes: 17 additions & 5 deletions src/Generator/ActiveRecord/default/model.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php
declare(strict_types=1);
/**
* This is the template for generating a controller class file.
*/

/* @var $command Yiisoft\Yii\Gii\Generator\ActiveRecord\Command */
declare(strict_types=1);

use Yiisoft\Strings\StringHelper;

/**
* @var Yiisoft\Yii\Gii\Generator\ActiveRecord\Command $command
* @var array<string, string> $properties
*/

echo "<?php\n";
?>

Expand All @@ -19,6 +20,17 @@

final class <?= $command->getModelName(); ?> extends <?= StringHelper::baseName($command->getBaseClass()) . PHP_EOL ?>
{
<?php foreach ($properties as $property): ?>
private <?=sprintf(
'%s%s $%s',
$property['isAllowNull'] ? '?' : '',

Check failure on line 26 in src/Generator/ActiveRecord/default/model.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidArrayOffset

src/Generator/ActiveRecord/default/model.php:26:9: InvalidArrayOffset: Cannot access value on variable $property using offset value of 'isAllowNull', expecting int (see https://psalm.dev/115)

Check failure on line 26 in src/Generator/ActiveRecord/default/model.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidArrayOffset

src/Generator/ActiveRecord/default/model.php:26:9: InvalidArrayOffset: Cannot access value on variable $property using offset value of 'isAllowNull', expecting int (see https://psalm.dev/115)

Check failure on line 26 in src/Generator/ActiveRecord/default/model.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidArrayOffset

src/Generator/ActiveRecord/default/model.php:26:9: InvalidArrayOffset: Cannot access value on variable $property using offset value of 'isAllowNull', expecting int (see https://psalm.dev/115)

Check failure on line 26 in src/Generator/ActiveRecord/default/model.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidArrayOffset

src/Generator/ActiveRecord/default/model.php:26:9: InvalidArrayOffset: Cannot access value on variable $property using offset value of 'isAllowNull', expecting int (see https://psalm.dev/115)
$property['type'],

Check failure on line 27 in src/Generator/ActiveRecord/default/model.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidArrayOffset

src/Generator/ActiveRecord/default/model.php:27:9: InvalidArrayOffset: Cannot access value on variable $property using offset value of 'type', expecting int (see https://psalm.dev/115)

Check failure on line 27 in src/Generator/ActiveRecord/default/model.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidArrayOffset

src/Generator/ActiveRecord/default/model.php:27:9: InvalidArrayOffset: Cannot access value on variable $property using offset value of 'type', expecting int (see https://psalm.dev/115)

Check failure on line 27 in src/Generator/ActiveRecord/default/model.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidArrayOffset

src/Generator/ActiveRecord/default/model.php:27:9: InvalidArrayOffset: Cannot access value on variable $property using offset value of 'type', expecting int (see https://psalm.dev/115)

Check failure on line 27 in src/Generator/ActiveRecord/default/model.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidArrayOffset

src/Generator/ActiveRecord/default/model.php:27:9: InvalidArrayOffset: Cannot access value on variable $property using offset value of 'type', expecting int (see https://psalm.dev/115)
$property['name'],

Check failure on line 28 in src/Generator/ActiveRecord/default/model.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidArrayOffset

src/Generator/ActiveRecord/default/model.php:28:9: InvalidArrayOffset: Cannot access value on variable $property using offset value of 'name', expecting int (see https://psalm.dev/115)

Check failure on line 28 in src/Generator/ActiveRecord/default/model.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidArrayOffset

src/Generator/ActiveRecord/default/model.php:28:9: InvalidArrayOffset: Cannot access value on variable $property using offset value of 'name', expecting int (see https://psalm.dev/115)

Check failure on line 28 in src/Generator/ActiveRecord/default/model.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidArrayOffset

src/Generator/ActiveRecord/default/model.php:28:9: InvalidArrayOffset: Cannot access value on variable $property using offset value of 'name', expecting int (see https://psalm.dev/115)

Check failure on line 28 in src/Generator/ActiveRecord/default/model.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidArrayOffset

src/Generator/ActiveRecord/default/model.php:28:9: InvalidArrayOffset: Cannot access value on variable $property using offset value of 'name', expecting int (see https://psalm.dev/115)
)?>;
<?php endforeach; ?>
<?php if (!empty($properties)) {
echo PHP_EOL;
} ?>
public function tableName(): string
{
return '<?= $command->getTableName() ?>';
Expand Down

0 comments on commit b08de00

Please sign in to comment.