From 68dd38dce3d64765f060caa10de370f74b3085c3 Mon Sep 17 00:00:00 2001 From: passchn <77938819+passchn@users.noreply.github.com> Date: Fri, 10 Mar 2023 22:01:27 +0100 Subject: [PATCH 1/4] Added `--allow-alias-relations` option for model command --- src/Command/ModelCommand.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Command/ModelCommand.php b/src/Command/ModelCommand.php index 8c72cef3..1cd6d869 100644 --- a/src/Command/ModelCommand.php +++ b/src/Command/ModelCommand.php @@ -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( @@ -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 Arguments|null $args * @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) { @@ -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('allow-alias-relations'); $found = $this->findTableReferencedBy($schema, $fieldName); - if (!$found) { + if ($found) { + $tmpModelName = Inflector::camelize($found); + } elseif (!$allowAliasRelations) { continue; } - $tmpModelName = Inflector::camelize($found); } $assoc = [ 'alias' => $tmpModelName, @@ -1319,6 +1322,9 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar ])->addOption('no-fixture', [ 'boolean' => true, 'help' => 'Do not generate a test fixture skeleton.', + ])->addOption('allow-alias-relations', [ + 'boolean' => true, + 'help' => 'Skip checks for existing tables of has one relations, e.g. for an example_id field.', ])->setEpilog( 'Omitting all arguments and options will list the table names you can generate models for.' ); From c974753716c7d4fa27009af919cd94e621826870 Mon Sep 17 00:00:00 2001 From: passchn <77938819+passchn@users.noreply.github.com> Date: Fri, 10 Mar 2023 22:20:39 +0100 Subject: [PATCH 2/4] Added test for allow-alias-relations option --- tests/TestCase/Command/ModelCommandTest.php | 53 +++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/tests/TestCase/Command/ModelCommandTest.php b/tests/TestCase/Command/ModelCommandTest.php index e7c5c679..051bcd13 100644 --- a/tests/TestCase/Command/ModelCommandTest.php +++ b/tests/TestCase/Command/ModelCommandTest.php @@ -490,6 +490,59 @@ public function testGetAssociationsAddAssociationIfTableExist() $this->assertEquals($expected, $result); } + /** + * Test that association generation adds `Anythings` association for `anything_id` field + * when using `--allow-alias-relations` 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([], ['allow-alias-relations' => 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 * From f9d06825c23b57ff05ac98024a6c86fbaa59dcc3 Mon Sep 17 00:00:00 2001 From: passchn <77938819+passchn@users.noreply.github.com> Date: Fri, 10 Mar 2023 22:39:17 +0100 Subject: [PATCH 3/4] fix code style --- src/Command/ModelCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Command/ModelCommand.php b/src/Command/ModelCommand.php index 1cd6d869..0653f058 100644 --- a/src/Command/ModelCommand.php +++ b/src/Command/ModelCommand.php @@ -329,7 +329,7 @@ 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 Arguments|null $args + * @param \Cake\Console\Arguments|null $args CLI arguments * @return array Associations with belongsTo added in. */ public function findBelongsTo(Table $model, array $associations, ?Arguments $args = null): array From b285ca9dc24ecd3819799294b9a5479c46ff5909 Mon Sep 17 00:00:00 2001 From: passchn <77938819+passchn@users.noreply.github.com> Date: Tue, 14 Mar 2023 20:00:08 +0100 Subject: [PATCH 4/4] rename option to "skip-relation-check" --- src/Command/ModelCommand.php | 7 ++++--- tests/TestCase/Command/ModelCommandTest.php | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Command/ModelCommand.php b/src/Command/ModelCommand.php index 0653f058..fc9b95ec 100644 --- a/src/Command/ModelCommand.php +++ b/src/Command/ModelCommand.php @@ -363,7 +363,7 @@ public function findBelongsTo(Table $model, array $associations, ?Arguments $arg get_class($associationTable) === Table::class && !in_array(Inflector::tableize($tmpModelName), $tables, true) ) { - $allowAliasRelations = $args && $args->getOption('allow-alias-relations'); + $allowAliasRelations = $args && $args->getOption('skip-relation-check'); $found = $this->findTableReferencedBy($schema, $fieldName); if ($found) { $tmpModelName = Inflector::camelize($found); @@ -1322,9 +1322,10 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar ])->addOption('no-fixture', [ 'boolean' => true, 'help' => 'Do not generate a test fixture skeleton.', - ])->addOption('allow-alias-relations', [ + ])->addOption('skip-relation-check', [ 'boolean' => true, - 'help' => 'Skip checks for existing tables of has one relations, e.g. for an example_id field.', + '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.' ); diff --git a/tests/TestCase/Command/ModelCommandTest.php b/tests/TestCase/Command/ModelCommandTest.php index 051bcd13..3080f58e 100644 --- a/tests/TestCase/Command/ModelCommandTest.php +++ b/tests/TestCase/Command/ModelCommandTest.php @@ -492,7 +492,7 @@ public function testGetAssociationsAddAssociationIfTableExist() /** * Test that association generation adds `Anythings` association for `anything_id` field - * when using `--allow-alias-relations` option, even if no db table exists + * when using `--skip-relation-check` option, even if no db table exists * * @return void */ @@ -504,7 +504,7 @@ public function testGetAssociationsAddAssociationIfNoTableExistButAliasIsAllowed $command = new ModelCommand(); $command->connection = 'test'; - $args = new Arguments([], ['allow-alias-relations' => true], []); + $args = new Arguments([], ['skip-relation-check' => true], []); $io = $this->createMock(ConsoleIo::class); $result = $command->getAssociations($items, $args, $io); $expected = [