Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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.
  • Loading branch information...
commit 9d7c97c2960980b1d0029079de2453ddaf86fde6 1 parent 4d702ee
@burzum burzum authored
View
9 lib/Cake/Model/Model.php
@@ -2102,7 +2102,7 @@ public function hasAny($conditions = null) {
* - 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 @@ public function find($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
+ 'offset' => null, 'order' => null, 'page' => 1, 'group' => null, 'callbacks' => true,
+ 'returnQuery' => false
),
(array)$query
);
@@ -2156,6 +2157,10 @@ public function find($type = 'first', $query = array()) {
}
}
+ if ($query['returnQuery'] == true) {
+ return $query;
+ }
+
$results = $this->getDataSource()->read($this, $query);
$this->resetAssociations();
View
28 lib/Cake/Test/Case/Model/ModelReadTest.php
@@ -6011,6 +6011,34 @@ public function testConditionalNumerics() {
}
/**
+ * 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
*
* @access public

0 comments on commit 9d7c97c

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