Skip to content

Commit

Permalink
Adding the "returnQuery" key to the 2nd argument of the find() method…
Browse files Browse the repository at this point in the history
… 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
Florian Kr�mer committed Jul 11, 2011
1 parent 4d702ee commit 9d7c97c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/Cake/Model/Model.php
Expand Up @@ -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
*/
Expand All @@ -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
);
Expand Down Expand Up @@ -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();

Expand Down
28 changes: 28 additions & 0 deletions lib/Cake/Test/Case/Model/ModelReadTest.php
Expand Up @@ -6010,6 +6010,34 @@ public function testConditionalNumerics() {
$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
*
Expand Down

0 comments on commit 9d7c97c

Please sign in to comment.