From 9d7c97c2960980b1d0029079de2453ddaf86fde6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=E4mer?= Date: Tue, 12 Jul 2011 00:54:24 +0200 Subject: [PATCH 1/2] Adding the "returnQuery" key to the 2nd argument of the find() method to be able to get the query array back from the before state of findMethod() calls. This was required in the past for some more complex queries and is in 2.0 no longer possible because the find methods became protected. --- lib/Cake/Model/Model.php | 9 +++++-- lib/Cake/Test/Case/Model/ModelReadTest.php | 28 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 3d7465cdc..53c1c15f8 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -2102,7 +2102,7 @@ class Model extends Object { * - Otherwise, first and second fields are used for key and value. * * @param string $type Type of find operation (all / first / count / neighbors / list / threaded) - * @param array $query Option fields (conditions / fields / joins / limit / offset / order / page / group / callbacks) + * @param array $query Option fields (conditions / fields / joins / limit / offset / order / page / group / callbacks / returnQuery) * @return array Array of records * @link http://book.cakephp.org/view/1018/find */ @@ -2113,7 +2113,8 @@ class Model extends Object { $query = array_merge( array( 'conditions' => null, 'fields' => null, 'joins' => array(), 'limit' => null, - 'offset' => null, 'order' => null, 'page' => 1, 'group' => null, 'callbacks' => true + 'offset' => null, 'order' => null, 'page' => 1, 'group' => null, 'callbacks' => true, + 'returnQuery' => false ), (array)$query ); @@ -2156,6 +2157,10 @@ class Model extends Object { } } + if ($query['returnQuery'] == true) { + return $query; + } + $results = $this->getDataSource()->read($this, $query); $this->resetAssociations(); diff --git a/lib/Cake/Test/Case/Model/ModelReadTest.php b/lib/Cake/Test/Case/Model/ModelReadTest.php index f6f17c528..324a62472 100644 --- a/lib/Cake/Test/Case/Model/ModelReadTest.php +++ b/lib/Cake/Test/Case/Model/ModelReadTest.php @@ -6010,6 +6010,34 @@ class ModelReadTest extends BaseModelTest { $this->assertTrue(empty($result)); } +/** + * test find() with the returnQuery opton in the 2nd argument to get the query array back + * + * @access public + * @return void + */ + public function testFindReturnQuery() { + $this->loadFixtures('User'); + $TestModel = new User(); + $TestModel->cacheQueries = false; + + $expected = array( + 'conditions' => array( + 'user' => 'larry'), + 'fields' => NULL, + 'joins' => array (), + 'limit' => NULL, + 'offset' => NULL, + 'order' => array( + 0 => NULL), + 'page' => 1, + 'group' => NULL, + 'callbacks' => true, + 'returnQuery' => true); + $result = $TestModel->find('all', array('returnQuery' => true, 'conditions' => array('user' => 'larry'))); + $this->assertEqual($expected, $result); + } + /** * test find('all') method * From adb943b7fda259947873dbafa8b2c8f02d341b0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=E4mer?= Date: Tue, 12 Jul 2011 23:31:07 +0200 Subject: [PATCH 2/2] Refactoring the code to build the query array as suggested here https://github.com/cakephp/cakephp/pull/147#issuecomment-1553663 --- lib/Cake/Model/Model.php | 58 +++++++++++++--------- lib/Cake/Test/Case/Model/ModelReadTest.php | 6 +-- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 53c1c15f8..b37cbc5f0 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -2102,7 +2102,7 @@ class Model extends Object { * - Otherwise, first and second fields are used for key and value. * * @param string $type Type of find operation (all / first / count / neighbors / list / threaded) - * @param array $query Option fields (conditions / fields / joins / limit / offset / order / page / group / callbacks / returnQuery) + * @param array $query Option fields (conditions / fields / joins / limit / offset / order / page / group / callbacks) * @return array Array of records * @link http://book.cakephp.org/view/1018/find */ @@ -2110,11 +2110,42 @@ class Model extends Object { $this->findQueryType = $type; $this->id = $this->getID(); + $query = $this->buildQuery($type, $query); + if (is_null($query)) { + return null; + } + + $results = $this->getDataSource()->read($this, $query); + $this->resetAssociations(); + + if ($query['callbacks'] === true || $query['callbacks'] === 'after') { + $results = $this->_filterResults($results); + } + + $this->findQueryType = null; + + if ($type === 'all') { + return $results; + } else { + if ($this->findMethods[$type] === true) { + return $this->{'_find' . ucfirst($type)}('after', $query, $results); + } + } + } + +/** + * Builds the query array that is used by the data source to generate the query to fetch the data. + * + * @param string $type Type of find operation (all / first / count / neighbors / list / threaded) + * @param array $query Option fields (conditions / fields / joins / limit / offset / order / page / group / callbacks) + * @return array Query array or null if it could not be build for some reasons + * @see Model::find() + */ + public function buildQuery($type = 'first', $query = array()) { $query = array_merge( array( 'conditions' => null, 'fields' => null, 'joins' => array(), 'limit' => null, 'offset' => null, 'order' => null, 'page' => 1, 'group' => null, 'callbacks' => true, - 'returnQuery' => false ), (array)$query ); @@ -2156,27 +2187,8 @@ class Model extends Object { return null; } } - - if ($query['returnQuery'] == true) { - return $query; - } - - $results = $this->getDataSource()->read($this, $query); - $this->resetAssociations(); - - if ($query['callbacks'] === true || $query['callbacks'] === 'after') { - $results = $this->_filterResults($results); - } - - $this->findQueryType = null; - - if ($type === 'all') { - return $results; - } else { - if ($this->findMethods[$type] === true) { - return $this->{'_find' . ucfirst($type)}('after', $query, $results); - } - } + + return $query; } /** diff --git a/lib/Cake/Test/Case/Model/ModelReadTest.php b/lib/Cake/Test/Case/Model/ModelReadTest.php index 324a62472..7d7579277 100644 --- a/lib/Cake/Test/Case/Model/ModelReadTest.php +++ b/lib/Cake/Test/Case/Model/ModelReadTest.php @@ -6011,12 +6011,12 @@ class ModelReadTest extends BaseModelTest { } /** - * test find() with the returnQuery opton in the 2nd argument to get the query array back + * test buildQuery() * * @access public * @return void */ - public function testFindReturnQuery() { + public function testBuildQuery() { $this->loadFixtures('User'); $TestModel = new User(); $TestModel->cacheQueries = false; @@ -6034,7 +6034,7 @@ class ModelReadTest extends BaseModelTest { 'group' => NULL, 'callbacks' => true, 'returnQuery' => true); - $result = $TestModel->find('all', array('returnQuery' => true, 'conditions' => array('user' => 'larry'))); + $result = $TestModel->buildQuery('all', array('returnQuery' => true, 'conditions' => array('user' => 'larry'))); $this->assertEqual($expected, $result); }