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.

This commit is contained in:
Florian Krmer 2011-07-12 00:54:24 +02:00
parent 4d702ee97f
commit 9d7c97c296
2 changed files with 35 additions and 2 deletions

View file

@ -2102,7 +2102,7 @@ class Model extends Object {
* - Otherwise, first and second fields are used for key and value. * - 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 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 * @return array Array of records
* @link http://book.cakephp.org/view/1018/find * @link http://book.cakephp.org/view/1018/find
*/ */
@ -2113,7 +2113,8 @@ class Model extends Object {
$query = array_merge( $query = array_merge(
array( array(
'conditions' => null, 'fields' => null, 'joins' => array(), 'limit' => null, '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 (array)$query
); );
@ -2156,6 +2157,10 @@ class Model extends Object {
} }
} }
if ($query['returnQuery'] == true) {
return $query;
}
$results = $this->getDataSource()->read($this, $query); $results = $this->getDataSource()->read($this, $query);
$this->resetAssociations(); $this->resetAssociations();

View file

@ -6010,6 +6010,34 @@ class ModelReadTest extends BaseModelTest {
$this->assertTrue(empty($result)); $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 * test find('all') method
* *