Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactoring the code to build the query array as suggested here #147 …

  • Loading branch information...
commit adb943b7fda259947873dbafa8b2c8f02d341b0b 1 parent 9d7c97c
@burzum burzum authored
View
58 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 / 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 @@ 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,
- 'returnQuery' => false
),
(array)$query
);
@@ -2156,27 +2187,8 @@ public function find($type = 'first', $query = array()) {
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;
}
/**
View
6 lib/Cake/Test/Case/Model/ModelReadTest.php
@@ -6011,12 +6011,12 @@ public function testConditionalNumerics() {
}
/**
- * 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 @@ public function testFindReturnQuery() {
'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);
}
Please sign in to comment.
Something went wrong with that request. Please try again.