Skip to content

Commit

Permalink
Fixing counter cache generation in bake
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jun 18, 2014
1 parent 4959ca6 commit 761da8b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
17 changes: 12 additions & 5 deletions src/Console/Command/Task/ModelTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,13 @@ public function findBelongsTo($model, array $associations) {
$associations['belongsTo'][] = [
'alias' => $tmpModelName,
'foreignKey' => $fieldName,
'table' => Inflector::underscore($tmpModelName)
];
} elseif ($fieldName === 'parent_id') {
$associations['belongsTo'][] = [
'alias' => 'Parent' . $model->alias(),
'foreignKey' => $fieldName,
'table' => $model->table()
];
}
}
Expand Down Expand Up @@ -528,15 +530,20 @@ public function getCounterCache($model) {
$counterCache = [];
foreach ($belongsTo['belongsTo'] as $otherTable) {
$otherAlias = $otherTable['alias'];
$otherModel = $this->getTableObject($this->_modelName($otherAlias), Inflector::underscore($otherAlias));
$otherSchema = $otherModel->schema();
$otherModel = $this->getTableObject($this->_modelName($otherAlias), $otherTable['table']);

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)) {
continue;
if (in_array($field, $otherFields, true)) {
$counterCache[] = "'{$otherAlias}' => ['{$field}']";
}
$counterCache[] = "'{$otherAlias}' => ['{$field}']";
}
return $counterCache;
}
Expand Down
18 changes: 14 additions & 4 deletions tests/TestCase/Console/Command/Task/ModelTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);

/**
Expand Down Expand Up @@ -244,6 +244,7 @@ public function testGetAssociations() {
[
'alias' => 'BakeUsers',
'foreignKey' => 'bake_user_id',
'table' => 'bake_users'
],
],
'hasMany' => [
Expand Down Expand Up @@ -277,10 +278,12 @@ public function testBelongsToGeneration() {
[
'alias' => 'BakeArticles',
'foreignKey' => 'bake_article_id',
'table' => 'bake_articles'
],
[
'alias' => 'BakeUsers',
'foreignKey' => 'bake_user_id',
'table' => 'bake_users'
],
]
];
Expand All @@ -293,6 +296,7 @@ public function testBelongsToGeneration() {
[
'alias' => 'ParentCategoryThreads',
'foreignKey' => 'parent_id',
'table' => 'category_threads'
],
]
];
Expand Down Expand Up @@ -557,10 +561,16 @@ public function testGetBehaviors() {
$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);
}
Expand Down Expand Up @@ -1001,7 +1011,7 @@ public function testSkipTablesAndAll() {
}

$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');
Expand Down

0 comments on commit 761da8b

Please sign in to comment.