Skip to content
This repository
Browse code

Prevent unneeded afterFind callback triggering on associated models. …

…Fixes #2057
  • Loading branch information...
commit 1244656595618b4a6b0e52d6742e32b5d74ffd23 1 parent 8473d6a
ADmad authored October 24, 2011
17  lib/Cake/Model/Datasource/DboSource.php
@@ -990,7 +990,7 @@ public function read(Model $model, $queryData = array(), $recursive = null) {
990 990
 		$queryData = $this->_scrubQueryData($queryData);
991 991
 
992 992
 		$null = null;
993  
-		$array = array();
  993
+		$array = array('callbacks' => $queryData['callbacks']);
994 994
 		$linkedModels = array();
995 995
 		$bypass = false;
996 996
 
@@ -1043,7 +1043,11 @@ public function read(Model $model, $queryData = array(), $recursive = null) {
1043 1043
 			return false;
1044 1044
 		}
1045 1045
 
1046  
-		$filtered = $this->_filterResults($resultSet, $model);
  1046
+		$filtered = array();
  1047
+
  1048
+		if ($queryData['callbacks'] === true || $queryData['callbacks'] === 'after') {
  1049
+			$filtered = $this->_filterResults($resultSet, $model);
  1050
+		}
1047 1051
 
1048 1052
 		if ($model->recursive > -1) {
1049 1053
 			foreach ($_associations as $type) {
@@ -1071,7 +1075,9 @@ public function read(Model $model, $queryData = array(), $recursive = null) {
1071 1075
 					}
1072 1076
 				}
1073 1077
 			}
1074  
-			$this->_filterResults($resultSet, $model, $filtered);
  1078
+			if ($queryData['callbacks'] === true || $queryData['callbacks'] === 'after') {
  1079
+				$this->_filterResults($resultSet, $model, $filtered);
  1080
+			}
1075 1081
 		}
1076 1082
 
1077 1083
 		if (!is_null($recursive)) {
@@ -1162,7 +1168,9 @@ public function queryAssociation($model, &$linkModel, $type, $association, $asso
1162 1168
 						}
1163 1169
 					}
1164 1170
 				}
1165  
-				$this->_filterResults($fetch, $model);
  1171
+				if ($queryData['callbacks'] === true || $queryData['callbacks'] === 'after') {
  1172
+					$this->_filterResults($fetch, $model);
  1173
+				}
1166 1174
 				return $this->_mergeHasMany($resultSet, $fetch, $association, $model, $linkModel);
1167 1175
 			} elseif ($type === 'hasAndBelongsToMany') {
1168 1176
 				$ins = $fetch = array();
@@ -2093,6 +2101,7 @@ protected function _scrubQueryData($data) {
2093 2101
 		static $base = null;
2094 2102
 		if ($base === null) {
2095 2103
 			$base = array_fill_keys(array('conditions', 'fields', 'joins', 'order', 'limit', 'offset', 'group'), array());
  2104
+			$base['callbacks'] = null;
2096 2105
 		}
2097 2106
 		return (array)$data + $base;
2098 2107
 	}
5  lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php
@@ -1002,7 +1002,8 @@ public function testGenerateAssociationQuerySelfJoin() {
1002 1002
 			'order' => array(),
1003 1003
 			'limit' => array(),
1004 1004
 			'offset' => array(),
1005  
-			'group' => array()
  1005
+			'group' => array(),
  1006
+			'callbacks' => null
1006 1007
 		);
1007 1008
 		$queryData['joins'][0]['table'] = $this->Dbo->fullTableName($queryData['joins'][0]['table']);
1008 1009
 		$this->assertEqual($queryData, $expected);
@@ -2554,7 +2555,7 @@ public function testSchema() {
2554 2555
  */
2555 2556
 	public function testDropSchemaNoSchema() {
2556 2557
 		$result = $this->Dbo->dropSchema(null);
2557  
-	}	
  2558
+	}
2558 2559
 
2559 2560
 /**
2560 2561
  * testOrderParsing method
89  lib/Cake/Test/Case/Model/ModelReadTest.php
@@ -4934,6 +4934,95 @@ public function testCallbackDisabling() {
4934 4934
 	}
4935 4935
 
4936 4936
 /**
  4937
+ * testAssociationAfterFindCallbacksDisabled method
  4938
+ *
  4939
+ * @return void
  4940
+ */
  4941
+	public function testAssociationAfterFindCalbacksDisabled() {
  4942
+		$this->loadFixtures('Post', 'Author', 'Comment');
  4943
+		$TestModel = new Post();
  4944
+		$result = $TestModel->find('all', array('callbacks' => false));
  4945
+		$expected = array(
  4946
+			array(
  4947
+				'Post' => array(
  4948
+					'id' => '1',
  4949
+					'author_id' => '1',
  4950
+					'title' => 'First Post',
  4951
+					'body' => 'First Post Body',
  4952
+					'published' => 'Y',
  4953
+					'created' => '2007-03-18 10:39:23',
  4954
+					'updated' => '2007-03-18 10:41:31'
  4955
+				),
  4956
+				'Author' => array(
  4957
+					'id' => '1',
  4958
+					'user' => 'mariano',
  4959
+					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  4960
+					'created' => '2007-03-17 01:16:23',
  4961
+					'updated' => '2007-03-17 01:18:31'
  4962
+			)),
  4963
+			array(
  4964
+				'Post' => array(
  4965
+					'id' => '2',
  4966
+					'author_id' => '3',
  4967
+					'title' => 'Second Post',
  4968
+					'body' => 'Second Post Body',
  4969
+					'published' => 'Y',
  4970
+					'created' => '2007-03-18 10:41:23',
  4971
+					'updated' => '2007-03-18 10:43:31'
  4972
+				),
  4973
+				'Author' => array(
  4974
+					'id' => '3',
  4975
+					'user' => 'larry',
  4976
+					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  4977
+					'created' => '2007-03-17 01:20:23',
  4978
+					'updated' => '2007-03-17 01:22:31'
  4979
+			)),
  4980
+			array(
  4981
+				'Post' => array(
  4982
+					'id' => '3',
  4983
+					'author_id' => '1',
  4984
+					'title' => 'Third Post',
  4985
+					'body' => 'Third Post Body',
  4986
+					'published' => 'Y',
  4987
+					'created' => '2007-03-18 10:43:23',
  4988
+					'updated' => '2007-03-18 10:45:31'
  4989
+				),
  4990
+				'Author' => array(
  4991
+					'id' => '1',
  4992
+					'user' => 'mariano',
  4993
+					'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  4994
+					'created' => '2007-03-17 01:16:23',
  4995
+					'updated' => '2007-03-17 01:18:31'
  4996
+		)));
  4997
+		$this->assertEqual($expected, $result);
  4998
+		unset($TestModel);
  4999
+
  5000
+		$Author = new Author();
  5001
+		$Author->Post->bindModel(array(
  5002
+			'hasMany' => array(
  5003
+				'Comment' => array(
  5004
+					'className' => 'ModifiedComment',
  5005
+					'foreignKey' => 'article_id',
  5006
+				)
  5007
+		)));
  5008
+		$result = $Author->find('all', array(
  5009
+			'conditions' => array('Author.id' => 1),
  5010
+			'recursive' => 2,
  5011
+			'callbacks' => false
  5012
+		));
  5013
+		$expected = array(
  5014
+			'id' => 1,
  5015
+			'article_id' => 1,
  5016
+			'user_id' => 2,
  5017
+			'comment' => 'First Comment for First Article',
  5018
+			'published' => 'Y',
  5019
+			'created' => '2007-03-18 10:45:23',
  5020
+			'updated' => '2007-03-18 10:47:31'
  5021
+		);
  5022
+		$this->assertEqual($result[0]['Post'][0]['Comment'][0], $expected);
  5023
+	}
  5024
+
  5025
+/**
4937 5026
  * Tests that the database configuration assigned to the model can be changed using
4938 5027
  * (before|after)Find callbacks
4939 5028
  *

0 notes on commit 1244656

Please sign in to comment.
Something went wrong with that request. Please try again.