Skip to content

Commit

Permalink
Prevent unneeded afterFind callback triggering on associated models. F…
Browse files Browse the repository at this point in the history
…ixes #2057
  • Loading branch information
ADmad committed Oct 26, 2011
1 parent 7c96f87 commit adb5be6
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 10 deletions.
19 changes: 15 additions & 4 deletions cake/libs/model/datasources/dbo_source.php
Expand Up @@ -776,7 +776,7 @@ function read(&$model, $queryData = array(), $recursive = null) {
$queryData = $this->__scrubQueryData($queryData);

$null = null;
$array = array();
$array = array('callbacks' => $queryData['callbacks']);
$linkedModels = array();
$this->__bypass = false;
$this->__booleans = array();
Expand Down Expand Up @@ -827,7 +827,11 @@ function read(&$model, $queryData = array(), $recursive = null) {
return false;
}

$filtered = $this->__filterResults($resultSet, $model);
$filtered = array();

if ($queryData['callbacks'] === true || $queryData['callbacks'] === 'after') {
$filtered = $this->__filterResults($resultSet, $model);
}

if ($model->recursive > -1) {
foreach ($_associations as $type) {
Expand Down Expand Up @@ -855,7 +859,9 @@ function read(&$model, $queryData = array(), $recursive = null) {
}
}
}
$this->__filterResults($resultSet, $model, $filtered);
if ($queryData['callbacks'] === true || $queryData['callbacks'] === 'after') {
$this->__filterResults($resultSet, $model, $filtered);
}
}

if (!is_null($recursive)) {
Expand Down Expand Up @@ -961,7 +967,9 @@ function queryAssociation(&$model, &$linkModel, $type, $association, $assocData,
}
}
}
$this->__filterResults($fetch, $model);
if ($queryData['callbacks'] === true || $queryData['callbacks'] === 'after') {
$this->__filterResults($fetch, $model);
}
return $this->__mergeHasMany($resultSet, $fetch, $association, $model, $linkModel, $recursive);
} elseif ($type === 'hasAndBelongsToMany') {
$ins = $fetch = array();
Expand Down Expand Up @@ -1915,6 +1923,9 @@ function __scrubQueryData($data) {
$data[$key] = array();
}
}
if (!array_key_exists('callbacks', $data)) {
$data['callbacks'] = null;
}
return $data;
}

Expand Down
11 changes: 6 additions & 5 deletions cake/tests/cases/libs/model/datasources/dbo_source.test.php
Expand Up @@ -1459,7 +1459,8 @@ function testGenerateAssociationQuerySelfJoin() {
'offset' => array(),
'conditions' => array(),
'order' => array(),
'group' => null
'group' => null,
'callbacks' => null
);
$queryData['joins'][0]['table'] = $this->testDb->fullTableName($queryData['joins'][0]['table']);
$this->assertEqual($queryData, $expected);
Expand Down Expand Up @@ -3523,7 +3524,7 @@ function testOrderParsing() {
$result = $this->testDb->order(array('Property.sale_price IS NULL'));
$expected = ' ORDER BY `Property`.`sale_price` IS NULL ASC';
$this->assertEqual($result, $expected);

$result = $this->testDb->order(array('Export.column-name' => 'ASC'));
$expected = ' ORDER BY `Export`.`column-name` ASC';
$this->assertEqual($result, $expected, 'Columns with -s are not working with order()');
Expand Down Expand Up @@ -4478,12 +4479,12 @@ function testVirtualFieldsFetch() {
*/
function testVirtualFieldsComplexRead() {
$this->loadFixtures('DataTest', 'Article', 'Comment');

$Article =& ClassRegistry::init('Article');
$commentTable = $this->db->fullTableName('comments');
$Article =& ClassRegistry::init('Article');
$Article->virtualFields = array(
'comment_count' => 'SELECT COUNT(*) FROM ' . $commentTable .
'comment_count' => 'SELECT COUNT(*) FROM ' . $commentTable .
' AS Comment WHERE Article.id = Comment.article_id'
);
$result = $Article->find('all');
Expand Down Expand Up @@ -4588,7 +4589,7 @@ function testFullTablePermutations() {
$Article->tablePrefix = 'tbl_';
$result = $this->testDb->fullTableName($Article, false);
$this->assertEqual($result, 'tbl_articles');

$Article->useTable = $Article->table = 'with spaces';
$Article->tablePrefix = '';
$result = $this->testDb->fullTableName($Article);
Expand Down
91 changes: 90 additions & 1 deletion cake/tests/cases/libs/model/model_read.test.php
Expand Up @@ -4791,7 +4791,7 @@ function testBindWithCustomPrimaryKey() {
}

/**
* test that calling unbindModel() with reset == true multiple times
* test that calling unbindModel() with reset == true multiple times
* leaves associations in the correct state.
*
* @return void
Expand Down Expand Up @@ -4962,6 +4962,95 @@ function testCallbackDisabling() {
$this->assertEqual($result, $expected);
}

/**
* testAssociationAfterFindCallbacksDisabled method
*
* @return void
*/
public function testAssociationAfterFindCalbacksDisabled() {
$this->loadFixtures('Post', 'Author', 'Comment');
$TestModel = new Post();
$result = $TestModel->find('all', array('callbacks' => false));
$expected = array(
array(
'Post' => array(
'id' => '1',
'author_id' => '1',
'title' => 'First Post',
'body' => 'First Post Body',
'published' => 'Y',
'created' => '2007-03-18 10:39:23',
'updated' => '2007-03-18 10:41:31'
),
'Author' => array(
'id' => '1',
'user' => 'mariano',
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
'created' => '2007-03-17 01:16:23',
'updated' => '2007-03-17 01:18:31'
)),
array(
'Post' => array(
'id' => '2',
'author_id' => '3',
'title' => 'Second Post',
'body' => 'Second Post Body',
'published' => 'Y',
'created' => '2007-03-18 10:41:23',
'updated' => '2007-03-18 10:43:31'
),
'Author' => array(
'id' => '3',
'user' => 'larry',
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
'created' => '2007-03-17 01:20:23',
'updated' => '2007-03-17 01:22:31'
)),
array(
'Post' => array(
'id' => '3',
'author_id' => '1',
'title' => 'Third Post',
'body' => 'Third Post Body',
'published' => 'Y',
'created' => '2007-03-18 10:43:23',
'updated' => '2007-03-18 10:45:31'
),
'Author' => array(
'id' => '1',
'user' => 'mariano',
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
'created' => '2007-03-17 01:16:23',
'updated' => '2007-03-17 01:18:31'
)));
$this->assertEqual($expected, $result);
unset($TestModel);

$Author = new Author();
$Author->Post->bindModel(array(
'hasMany' => array(
'Comment' => array(
'className' => 'ModifiedComment',
'foreignKey' => 'article_id',
)
)));
$result = $Author->find('all', array(
'conditions' => array('Author.id' => 1),
'recursive' => 2,
'callbacks' => false
));
$expected = array(
'id' => 1,
'article_id' => 1,
'user_id' => 2,
'comment' => 'First Comment for First Article',
'published' => 'Y',
'created' => '2007-03-18 10:45:23',
'updated' => '2007-03-18 10:47:31'
);
$this->assertEqual($result[0]['Post'][0]['Comment'][0], $expected);
}

/**
* Tests that the database configuration assigned to the model can be changed using
* (before|after)Find callbacks
Expand Down

0 comments on commit adb5be6

Please sign in to comment.