Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add allow-alias-relations option to bake model command #913

Merged
merged 4 commits into from Mar 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/Command/ModelCommand.php
Expand Up @@ -233,7 +233,7 @@ public function getAssociations(Table $table, Arguments $args, ConsoleIo $io): a
];

$primary = $table->getPrimaryKey();
$associations = $this->findBelongsTo($table, $associations);
$associations = $this->findBelongsTo($table, $associations, $args);

if (is_array($primary) && count($primary) > 1) {
$io->warning(
Expand Down Expand Up @@ -329,9 +329,10 @@ public function getAssociationInfo(Table $table): array
*
* @param \Cake\ORM\Table $model Database\Table instance of table being generated.
* @param array $associations Array of in progress associations
* @param \Cake\Console\Arguments|null $args CLI arguments
* @return array Associations with belongsTo added in.
*/
public function findBelongsTo(Table $model, array $associations): array
public function findBelongsTo(Table $model, array $associations, ?Arguments $args = null): array
{
$schema = $model->getSchema();
foreach ($schema->columns() as $fieldName) {
Expand Down Expand Up @@ -362,11 +363,13 @@ public function findBelongsTo(Table $model, array $associations): array
get_class($associationTable) === Table::class &&
!in_array(Inflector::tableize($tmpModelName), $tables, true)
) {
$allowAliasRelations = $args && $args->getOption('skip-relation-check');
$found = $this->findTableReferencedBy($schema, $fieldName);
if (!$found) {
if ($found) {
$tmpModelName = Inflector::camelize($found);
} elseif (!$allowAliasRelations) {
continue;
}
$tmpModelName = Inflector::camelize($found);
}
$assoc = [
'alias' => $tmpModelName,
Expand Down Expand Up @@ -1319,6 +1322,10 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
])->addOption('no-fixture', [
'boolean' => true,
'help' => 'Do not generate a test fixture skeleton.',
])->addOption('skip-relation-check', [
'boolean' => true,
'help' => 'Generate relations for all "example_id" fields'
. ' without checking the database if a table "examples" exists.',
])->setEpilog(
'Omitting all arguments and options will list the table names you can generate models for.'
);
Expand Down
53 changes: 53 additions & 0 deletions tests/TestCase/Command/ModelCommandTest.php
Expand Up @@ -490,6 +490,59 @@ public function testGetAssociationsAddAssociationIfTableExist()
$this->assertEquals($expected, $result);
}

/**
* Test that association generation adds `Anythings` association for `anything_id` field
* when using `--skip-relation-check` option, even if no db table exists
*
* @return void
*/
public function testGetAssociationsAddAssociationIfNoTableExistButAliasIsAllowed()
{
$items = $this->getTableLocator()->get('TodoItems');

$items->setSchema($items->getSchema()->addColumn('anything_id', ['type' => 'integer']));
$command = new ModelCommand();
$command->connection = 'test';

$args = new Arguments([], ['skip-relation-check' => true], []);
$io = $this->createMock(ConsoleIo::class);
$result = $command->getAssociations($items, $args, $io);
$expected = [
'belongsTo' => [
[
'alias' => 'Users',
'foreignKey' => 'user_id',
'joinType' => 'INNER',
],
[
'alias' => 'Anythings',
'foreignKey' => 'anything_id',
],
],
'hasMany' => [
[
'alias' => 'TodoTasks',
'foreignKey' => 'todo_item_id',
],
],
'belongsToMany' => [
[
'alias' => 'TodoLabels',
'foreignKey' => 'todo_item_id',
'joinTable' => 'todo_items_todo_labels',
'targetForeignKey' => 'todo_label_id',
],
],
'hasOne' => [
[
'alias' => 'TodoReminders',
'foreignKey' => 'todo_item_id',
],
],
];
$this->assertEquals($expected, $result);
}

/**
* Test that association generation ignores `_id` fields
*
Expand Down