Skip to content

Commit

Permalink
Merge pull request #147 from burzum/feature/2.0-return-query
Browse files Browse the repository at this point in the history
Feature/2.0 return query
  • Loading branch information
lorenzo committed Jul 15, 2011
2 parents fb264d9 + adb943b commit 57a8c10
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
53 changes: 35 additions & 18 deletions lib/Cake/Model/Model.php
Expand Up @@ -2110,10 +2110,42 @@ public function find($type = 'first', $query = array()) {
$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
'offset' => null, 'order' => null, 'page' => 1, 'group' => null, 'callbacks' => true,
),
(array)$query
);
Expand Down Expand Up @@ -2155,23 +2187,8 @@ public function find($type = 'first', $query = array()) {
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);
}
}

return $query;
}

/**
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 buildQuery()
*
* @access public
* @return void
*/
public function testBuildQuery() {
$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->buildQuery('all', array('returnQuery' => true, 'conditions' => array('user' => 'larry')));
$this->assertEqual($expected, $result);
}

/**
* test find('all') method
*
Expand Down

0 comments on commit 57a8c10

Please sign in to comment.