Browse Source

BelongsTo associations are safe to bake even for composite keys

Fixes #4318
Jose Lorenzo Rodriguez 11 years ago
parent
commit
c77b3517cf

+ 3 - 2
src/Console/Command/Task/ModelTask.php

@@ -172,6 +172,8 @@ class ModelTask extends BakeTask {
 		];
 
 		$primary = $table->primaryKey();
+		$associations = $this->findBelongsTo($table, $associations);
+
 		if (is_array($primary) && count($primary) > 1) {
 			$this->err(
 				'<warning>Bake cannot generate associations for composite primary keys at this time</warning>.'
@@ -179,7 +181,6 @@ class ModelTask extends BakeTask {
 			return $associations;
 		}
 
-		$associations = $this->findBelongsTo($table, $associations);
 		$associations = $this->findHasMany($table, $associations);
 		$associations = $this->findBelongsToMany($table, $associations);
 		return $associations;
@@ -222,7 +223,7 @@ class ModelTask extends BakeTask {
 		foreach ($schema->columns() as $fieldName) {
 			$offset = strpos($fieldName, '_id');
 			$assoc = false;
-			if (!in_array($fieldName, $primary) && $fieldName !== 'parent_id' && $offset !== false) {
+			if ($fieldName !== 'parent_id' && $offset !== false) {
 				$tmpModelName = $this->_modelNameFromKey($fieldName);
 				$assoc = [
 					'alias' => $tmpModelName,

+ 46 - 12
tests/TestCase/Console/Command/Task/ModelTaskTest.php

@@ -37,7 +37,7 @@ class ModelTaskTest extends TestCase {
 	public $fixtures = array(
 		'core.bake_article', 'core.bake_comment', 'core.bake_articles_bake_tag',
 		'core.bake_tag', 'core.user', 'core.category_thread', 'core.number_tree',
-		'core.counter_cache_user', 'core.counter_cache_post'
+		'core.counter_cache_user', 'core.counter_cache_post', 'core.articles_tag'
 	);
 
 /**
@@ -352,6 +352,30 @@ class ModelTaskTest extends TestCase {
 	}
 
 /**
+ * Test that belongsTo generation works for models with composite
+ * primary keys
+ *
+ * @return void
+ */
+	public function testBelongsToGenerationCompositeKey() {
+		$model = TableRegistry::get('ArticlesTags');
+		$result = $this->Task->findBelongsTo($model, []);
+		$expected = [
+			'belongsTo' => [
+				[
+					'alias' => 'Articles',
+					'foreignKey' => 'article_id'
+				],
+				[
+					'alias' => 'Tags',
+					'foreignKey' => 'tag_id'
+				]
+			]
+		];
+		$this->assertEquals($expected, $result);
+	}
+
+/**
  * test that hasOne and/or hasMany relations are generated properly.
  *
  * @return void
@@ -1007,53 +1031,63 @@ class ModelTaskTest extends TestCase {
 		$this->Task->Test->expects($this->exactly($count))
 			->method('bake');
 
+		$filename = $this->_normalizePath(APP . 'Model/Table/ArticlesTagsTable.php');
+		$this->Task->expects($this->at(1))
+			->method('createFile')
+			->with($filename, $this->stringContains('class ArticlesTagsTable extends'));
+
+		$filename = $this->_normalizePath(APP . 'Model/Entity/ArticlesTag.php');
+		$this->Task->expects($this->at(2))
+			->method('createFile')
+			->with($filename, $this->stringContains('class ArticlesTag extends'));
+
 		$filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
-		$this->Task->expects($this->at(0))
+		$this->Task->expects($this->at(3))
 			->method('createFile')
 			->with($filename, $this->stringContains('class BakeArticlesTable extends'));
 
 		$filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
-		$this->Task->expects($this->at(1))
+		$this->Task->expects($this->at(4))
 			->method('createFile')
 			->with($filename, $this->stringContains('class BakeArticle extends'));
 
 		$filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesBakeTagsTable.php');
-		$this->Task->expects($this->at(2))
+		$this->Task->expects($this->at(5))
 			->method('createFile')
 			->with($filename, $this->stringContains('class BakeArticlesBakeTagsTable extends'));
 
 		$filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticlesBakeTag.php');
-		$this->Task->expects($this->at(3))
+		$this->Task->expects($this->at(6))
 			->method('createFile')
 			->with($filename, $this->stringContains('class BakeArticlesBakeTag extends'));
 
 		$filename = $this->_normalizePath(APP . 'Model/Table/BakeCommentsTable.php');
-		$this->Task->expects($this->at(4))
+		$this->Task->expects($this->at(7))
 			->method('createFile')
 			->with($filename, $this->stringContains('class BakeCommentsTable extends'));
 
 		$filename = $this->_normalizePath(APP . 'Model/Entity/BakeComment.php');
-		$this->Task->expects($this->at(5))
+		$this->Task->expects($this->at(8))
 			->method('createFile')
 			->with($filename, $this->stringContains('class BakeComment extends'));
 
 		$filename = $this->_normalizePath(APP . 'Model/Table/BakeTagsTable.php');
-		$this->Task->expects($this->at(6))
+		$this->Task->expects($this->at(9))
 			->method('createFile')
 			->with($filename, $this->stringContains('class BakeTagsTable extends'));
 
 		$filename = $this->_normalizePath(APP . 'Model/Entity/BakeTag.php');
-		$this->Task->expects($this->at(7))
+		$this->Task->expects($this->at(10))
 			->method('createFile')
 			->with($filename, $this->stringContains('class BakeTag extends'));
 
 		$filename = $this->_normalizePath(APP . 'Model/Table/CategoryThreadsTable.php');
-		$this->Task->expects($this->at(8))
+		$this->Task->expects($this->at(11))
 			->method('createFile')
 			->with($filename, $this->stringContains('class CategoryThreadsTable extends'));
 
 		$filename = $this->_normalizePath(APP . 'Model/Entity/CategoryThread.php');
-		$this->Task->expects($this->at(9))
+		$this->Task->expects($this->at(12))
 			->method('createFile')
 			->with($filename, $this->stringContains('class CategoryThread extends'));
 
@@ -1072,7 +1106,7 @@ class ModelTaskTest extends TestCase {
 		}
 
 		$this->Task->connection = 'test';
-		$this->Task->skipTables = ['bake_tags', 'counter_cache_posts'];
+		$this->Task->skipTables = ['articles_tags', 'bake_tags', 'counter_cache_posts'];
 
 		$this->Task->Fixture->expects($this->exactly(7))
 			->method('bake');