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

Avoid generating association if table object or database table exist #842

Merged
merged 4 commits into from Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 11 additions & 3 deletions src/Command/ModelCommand.php
Expand Up @@ -346,11 +346,19 @@ public function findBelongsTo(Table $model, array $associations): array
];
} else {
$tmpModelName = $this->_modelNameFromKey($fieldName);
if (!in_array(Inflector::tableize($tmpModelName), $this->_tables, true)) {
$this->getTableLocator()->get($tmpModelName);
$genericInstances = $this->getTableLocator()->genericInstances();
ajibarra marked this conversation as resolved.
Show resolved Hide resolved
$tables = $this->listAll();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to use $this->_tables here as it was used here before.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

listAll() returns the value of $_tables if it's already populated else fetches the list from the db. I think using the method is fine here. That way it doesn't need to rely on listAll() being called before this method is invoked.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct. I initially used $this->_tables but found unit tests failed when findBelongsTo was called isolated. generateAssociations calls listAll as well as findHasMany and findBelongsToMany methods but findBelongsTo didn't.

/** Check if association model could not be instantiated as a subclass but a generic Table instance instead. */
ajibarra marked this conversation as resolved.
Show resolved Hide resolved
if (
isset($genericInstances[$tmpModelName]) &&
!in_array(Inflector::tableize($tmpModelName), $tables, true)
) {
$found = $this->findTableReferencedBy($schema, $fieldName);
if ($found) {
$tmpModelName = Inflector::camelize($found);
if (!$found) {
continue;
}
$tmpModelName = Inflector::camelize($found);
}
$assoc = [
'alias' => $tmpModelName,
Expand Down
92 changes: 92 additions & 0 deletions tests/TestCase/Command/ModelCommandTest.php
Expand Up @@ -375,6 +375,96 @@ public function testGetAssociationsPlugin()
$this->assertEquals($expected, $result);
}

/**
* Test that association generation ignores `anything_id` fields if
* AnythingsTable object nor `anythings` database table exist
*
* @return void
*/
public function testGetAssociationsIgnoreUnderscoreIdIfNoDbTable()
{
$items = $this->getTableLocator()->get('TodoItems');

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

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

/**
* Test that association generation adds association when `anything_id` fields and
* AnythingsTable object exist even if no db table
*
* @return void
*/
public function testGetAssociationsAddAssociationIfTableExist()
{
$items = $this->getTableLocator()->get('TodoItems');

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

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

/**
* Test that association generation ignores `_id` fields
*
Expand Down Expand Up @@ -491,6 +581,7 @@ public function testBelongsToGenerationConstraints()
{
$model = $this->getTableLocator()->get('Invitations');
$command = new ModelCommand();
$command->connection = 'test';
$result = $command->findBelongsTo($model, []);
$expected = [
'belongsTo' => [
Expand Down Expand Up @@ -519,6 +610,7 @@ public function testBelongsToGenerationCompositeKey()
{
$model = $this->getTableLocator()->get('TodoItemsTodoLabels');
$command = new ModelCommand();
$command->connection = 'test';
$result = $command->findBelongsTo($model, []);
$expected = [
'belongsTo' => [
Expand Down