Skip to content

Commit

Permalink
Avoid creating association if database table or table object don't ex…
Browse files Browse the repository at this point in the history
  • Loading branch information
ajibarra committed Jul 15, 2022
1 parent 2078952 commit e8c7cb4
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Command/ModelCommand.php
Expand Up @@ -30,6 +30,7 @@
use Cake\Database\Schema\TableSchemaInterface;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;

/**
Expand Down Expand Up @@ -346,11 +347,16 @@ 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();
$tables = $this->listAll();
/** Check if association model could not be instantiated as a subclass but a generic Table instance instead. */
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

0 comments on commit e8c7cb4

Please sign in to comment.