Browse Source

Merge pull request #3747 from cakephp/ceeram-3.0-bake-countercache

3.0 - Fixed countercache bake
Mark Story 11 years ago
parent
commit
cb6950c810

+ 32 - 12
src/Console/Command/Task/ModelTask.php

@@ -225,12 +225,12 @@ class ModelTask extends BakeTask {
 				$tmpModelName = $this->_modelNameFromKey($fieldName);
 				$associations['belongsTo'][] = [
 					'alias' => $tmpModelName,
-					'foreignKey' => $fieldName,
+					'foreignKey' => $fieldName
 				];
 			} elseif ($fieldName === 'parent_id') {
 				$associations['belongsTo'][] = [
 					'alias' => 'Parent' . $model->alias(),
-					'foreignKey' => $fieldName,
+					'foreignKey' => $fieldName
 				];
 			}
 		}
@@ -291,7 +291,6 @@ class ModelTask extends BakeTask {
  */
 	public function findBelongsToMany($model, array $associations) {
 		$schema = $model->schema();
-		$primaryKey = (array)$schema->primaryKey();
 		$tableName = $schema->name();
 		$foreignKey = $this->_modelKey($tableName);
 
@@ -511,15 +510,7 @@ class ModelTask extends BakeTask {
 			$behaviors['Tree'] = [];
 		}
 
-		$counterCache = [];
-		foreach ($fields as $field) {
-			if (strpos($field, '_count') === false) {
-				continue;
-			}
-			list($name) = explode('_count', $field);
-			$assoc = $this->_modelName($name);
-			$counterCache[] = "'{$assoc}' => ['{$field}']";
-		}
+		$counterCache = $this->getCounterCache($model);
 		if (!empty($counterCache)) {
 			$behaviors['CounterCache'] = $counterCache;
 		}
@@ -527,6 +518,35 @@ class ModelTask extends BakeTask {
 	}
 
 /**
+ * Get CounterCaches
+ *
+ * @param \Cake\ORM\Table $model
+ * @return array CounterCache configurations
+ */
+	public function getCounterCache($model) {
+		$belongsTo = $this->findBelongsTo($model, ['belongsTo' => []]);
+		$counterCache = [];
+		foreach ($belongsTo['belongsTo'] as $otherTable) {
+			$otherAlias = $otherTable['alias'];
+			$otherModel = $this->getTableObject($this->_modelName($otherAlias), Inflector::underscore($otherAlias));
+
+			try {
+				$otherSchema = $otherModel->schema();
+			} catch (\Cake\Database\Exception $e) {
+				continue;
+			}
+
+			$otherFields = $otherSchema->columns();
+			$alias = $model->alias();
+			$field = Inflector::singularize(Inflector::underscore($alias)) . '_count';
+			if (in_array($field, $otherFields, true)) {
+				$counterCache[] = "'{$otherAlias}' => ['{$field}']";
+			}
+		}
+		return $counterCache;
+	}
+
+/**
  * Bake an entity class.
  *
  * @param \Cake\ORM\Table $model Model name or object

+ 14 - 8
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_user', 'core.counter_cache_post'
 	);
 
 /**
@@ -243,7 +243,7 @@ class ModelTaskTest extends TestCase {
 			'belongsTo' => [
 				[
 					'alias' => 'BakeUsers',
-					'foreignKey' => 'bake_user_id',
+					'foreignKey' => 'bake_user_id'
 				],
 			],
 			'hasMany' => [
@@ -276,11 +276,11 @@ class ModelTaskTest extends TestCase {
 			'belongsTo' => [
 				[
 					'alias' => 'BakeArticles',
-					'foreignKey' => 'bake_article_id',
+					'foreignKey' => 'bake_article_id'
 				],
 				[
 					'alias' => 'BakeUsers',
-					'foreignKey' => 'bake_user_id',
+					'foreignKey' => 'bake_user_id'
 				],
 			]
 		];
@@ -292,7 +292,7 @@ class ModelTaskTest extends TestCase {
 			'belongsTo' => [
 				[
 					'alias' => 'ParentCategoryThreads',
-					'foreignKey' => 'parent_id',
+					'foreignKey' => 'parent_id'
 				],
 			]
 		];
@@ -557,10 +557,16 @@ class ModelTaskTest extends TestCase {
 		$result = $this->Task->getBehaviors($model);
 		$this->assertEquals(['Timestamp' => []], $result);
 
-		$model = TableRegistry::get('CounterCacheUsers');
+		TableRegistry::clear();
+		TableRegistry::get('Users', [
+			'table' => 'counter_cache_users'
+		]);
+		$model = TableRegistry::get('Posts', [
+			'table' => 'counter_cache_posts'
+		]);
 		$result = $this->Task->getBehaviors($model);
 		$expected = [
-			'CounterCache' => ["'Posts' => ['post_count']"]
+			'CounterCache' => ["'Users' => ['post_count']"]
 		];
 		$this->assertEquals($expected, $result);
 	}
@@ -1001,7 +1007,7 @@ class ModelTaskTest extends TestCase {
 		}
 
 		$this->Task->connection = 'test';
-		$this->Task->skipTables = ['bake_tags'];
+		$this->Task->skipTables = ['bake_tags', 'counter_cache_posts'];
 
 		$this->Task->Fixture->expects($this->exactly(7))
 			->method('bake');