From adb5be6ba7eabfdd0aefc80778f4be6e071c4670 Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 24 Oct 2011 00:33:29 +0530 Subject: [PATCH] Prevent unneeded afterFind callback triggering on associated models. Fixes #2057 --- cake/libs/model/datasources/dbo_source.php | 19 +++- .../model/datasources/dbo_source.test.php | 11 ++- .../cases/libs/model/model_read.test.php | 91 ++++++++++++++++++- 3 files changed, 111 insertions(+), 10 deletions(-) diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index ee19d01b9f7..16a3e51c439 100755 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.php @@ -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(); @@ -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) { @@ -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)) { @@ -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(); @@ -1915,6 +1923,9 @@ function __scrubQueryData($data) { $data[$key] = array(); } } + if (!array_key_exists('callbacks', $data)) { + $data['callbacks'] = null; + } return $data; } diff --git a/cake/tests/cases/libs/model/datasources/dbo_source.test.php b/cake/tests/cases/libs/model/datasources/dbo_source.test.php index 32ec6737c49..abf79ad374b 100644 --- a/cake/tests/cases/libs/model/datasources/dbo_source.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo_source.test.php @@ -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); @@ -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()'); @@ -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'); @@ -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); diff --git a/cake/tests/cases/libs/model/model_read.test.php b/cake/tests/cases/libs/model/model_read.test.php index bc8cade730b..2d713fc134d 100755 --- a/cake/tests/cases/libs/model/model_read.test.php +++ b/cake/tests/cases/libs/model/model_read.test.php @@ -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 @@ -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