Skip to content

Commit

Permalink
Merge pull request #68 from cakephp/issue-6270
Browse files Browse the repository at this point in the history
Ignore associations that point at missing tables.
  • Loading branch information
markstory committed Apr 8, 2015
2 parents 919fd4f + a47a633 commit 4cf1c99
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
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);
}
}

0 comments on commit 4cf1c99

Please sign in to comment.