From a47a6336946ca24052d2b0fd8d61da259e218c06 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Mon, 6 Apr 2015 10:20:11 -0400 Subject: [PATCH] Ignore associations that point at missing tables. Bake shouldn't error out when an association references a table that doesn't exist. Instead that association should be filtered out. Refs cakephp/cakephp#6270 --- src/Utility/Model/AssociationFilter.php | 25 +++++++++++-------- .../Utility/Model/AssociationFilterTest.php | 19 ++++++++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/Utility/Model/AssociationFilter.php b/src/Utility/Model/AssociationFilter.php index 966c85fe0..705b13e20 100644 --- a/src/Utility/Model/AssociationFilter.php +++ b/src/Utility/Model/AssociationFilter.php @@ -16,6 +16,7 @@ use Cake\ORM\Table; use Cake\Utility\Inflector; +use Exception; /** * Utility class to filter Model Table associations @@ -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; diff --git a/tests/TestCase/Utility/Model/AssociationFilterTest.php b/tests/TestCase/Utility/Model/AssociationFilterTest.php index 69ddf24ab..db830f926 100644 --- a/tests/TestCase/Utility/Model/AssociationFilterTest.php +++ b/tests/TestCase/Utility/Model/AssociationFilterTest.php @@ -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', @@ -59,6 +61,7 @@ public function setUp() */ public function tearDown() { + TableRegistry::clear(); unset($this->associationFilter); parent::tearDown(); } @@ -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); + } }