Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/Utility/Model/AssociationFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Cake\ORM\Table;
use Cake\Utility\Inflector;
use Exception;

/**
* Utility class to filter Model Table associations
Expand Down Expand Up @@ -87,16 +88,20 @@ public function filterAssociations(Table $model)
$className = $alias;
}

$associations[$type][$assocName] = [
'property' => $assoc->property(),
'variable' => Inflector::variable($assocName),
'primaryKey' => (array)$target->primaryKey(),
'displayField' => $target->displayField(),
'foreignKey' => $assoc->foreignKey(),
'alias' => $alias,
'controller' => $className,
'fields' => $target->schema()->columns(),
];
try {
$associations[$type][$assocName] = [
'property' => $assoc->property(),
'variable' => Inflector::variable($assocName),
'primaryKey' => (array)$target->primaryKey(),
'displayField' => $target->displayField(),
'foreignKey' => $assoc->foreignKey(),
'alias' => $alias,
'controller' => $className,
'fields' => $target->schema()->columns(),
];
} catch (Exception $e) {
// Do nothing it could be a bogus association name.
}
}
}
return $associations;
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase/Utility/Model/AssociationFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class AssociationFilterTest extends TestCase
* @var array
*/
public $fixtures = [
'core.authors',
'core.tags',
'plugin.bake.bake_articles',
'plugin.bake.bake_comments',
'plugin.bake.bake_articles_bake_tags',
Expand All @@ -59,6 +61,7 @@ public function setUp()
*/
public function tearDown()
{
TableRegistry::clear();
unset($this->associationFilter);
parent::tearDown();
}
Expand Down Expand Up @@ -130,4 +133,20 @@ public function testFilterAssociations()
$expected = ['authors', 'tags'];
$this->assertEquals($expected, $result);
}

/**
* testFilterAssociations
*
* @return void
*/
public function testFilterAssociationsMissingTable()
{
$table = TableRegistry::get('Articles', [
'className' => '\Bake\Test\App\Model\Table\ArticlesTable'
]);
$table->hasMany('Nopes');

$result = $this->associationFilter->filterAssociations($table);
$this->assertArrayNotHasKey('HasMany', $result);
}
}