Skip to content

Commit

Permalink
Bake table with enums mapped.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Dec 23, 2023
1 parent 3f5780a commit b6af28f
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/Command/ModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ public function bakeTable(Table $model, array $data, Arguments $args, ConsoleIo
'validation' => [],
'rulesChecker' => [],
'behaviors' => [],
'enums' => $this->enums($data, $entity, $namespace),
'enums' => $this->enums($model, $entity, $namespace),
'connection' => $this->connection,
'fileBuilder' => new FileBuilder($io, "{$namespace}\Model\Table", $parsedFile),
];
Expand Down Expand Up @@ -1385,14 +1385,14 @@ public function bakeTest(string $className, Arguments $args, ConsoleIo $io): voi
}

/**
* @param array<string, mixed> $data
* @param \Cake\ORM\Table $table
* @param string $entity
* @param string $namespace
* @return array<string, class-string>
*/
protected function enums(array $data, string $entity, string $namespace): array
protected function enums(Table $table, string $entity, string $namespace): array
{
$fields = $data['fields'];
$fields = $this->enumLikeFields($table->getSchema());
$enumClassNamespace = $namespace . '\Model\Enum\\';

$enums = [];
Expand All @@ -1407,4 +1407,24 @@ protected function enums(array $data, string $entity, string $namespace): array

return $enums;
}

/**
* @param \Cake\Database\Schema\TableSchemaInterface $schema
* @return array<string>
*/
protected function enumLikeFields(TableSchemaInterface $schema): array
{
$fields = [];

foreach ($schema->columns() as $column) {
$columnSchema = $schema->getColumn($column);
if (!in_array($columnSchema['type'], ['string', 'tinyinteger'], true)) {
continue;
}

$fields[] = $column;
}

return $fields;
}
}

0 comments on commit b6af28f

Please sign in to comment.