Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Prevent unneeded afterFind callback triggering on associated models. …

…Fixes #2057
  • Loading branch information...
commit 1244656595618b4a6b0e52d6742e32b5d74ffd23 1 parent 8473d6a
@ADmad ADmad authored
View
17 lib/Cake/Model/Datasource/DboSource.php
@@ -990,7 +990,7 @@ public function read(Model $model, $queryData = array(), $recursive = null) {
$queryData = $this->_scrubQueryData($queryData);
$null = null;
- $array = array();
+ $array = array('callbacks' => $queryData['callbacks']);
$linkedModels = array();
$bypass = false;
@@ -1043,7 +1043,11 @@ public function read(Model $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) {
@@ -1071,7 +1075,9 @@ public function read(Model $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)) {
@@ -1162,7 +1168,9 @@ public function queryAssociation($model, &$linkModel, $type, $association, $asso
}
}
}
- $this->_filterResults($fetch, $model);
+ if ($queryData['callbacks'] === true || $queryData['callbacks'] === 'after') {
+ $this->_filterResults($fetch, $model);
+ }
return $this->_mergeHasMany($resultSet, $fetch, $association, $model, $linkModel);
} elseif ($type === 'hasAndBelongsToMany') {
$ins = $fetch = array();
@@ -2093,6 +2101,7 @@ protected function _scrubQueryData($data) {
static $base = null;
if ($base === null) {
$base = array_fill_keys(array('conditions', 'fields', 'joins', 'order', 'limit', 'offset', 'group'), array());
+ $base['callbacks'] = null;
}
return (array)$data + $base;
}
View
5 lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php
@@ -1002,7 +1002,8 @@ public function testGenerateAssociationQuerySelfJoin() {
'order' => array(),
'limit' => array(),
'offset' => array(),
- 'group' => array()
+ 'group' => array(),
+ 'callbacks' => null
);
$queryData['joins'][0]['table'] = $this->Dbo->fullTableName($queryData['joins'][0]['table']);
$this->assertEqual($queryData, $expected);
@@ -2554,7 +2555,7 @@ public function testSchema() {
*/
public function testDropSchemaNoSchema() {
$result = $this->Dbo->dropSchema(null);
- }
+ }
/**
* testOrderParsing method
View
89 lib/Cake/Test/Case/Model/ModelReadTest.php
@@ -4934,6 +4934,95 @@ public function testCallbackDisabling() {
}
/**
+ * 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
*
Please sign in to comment.
Something went wrong with that request. Please try again.