Permalink
Browse files

Merge pull request #917 from bar/master-find

Make Model::find('first') always return an array.
  • Loading branch information...
2 parents 20483a2 + c741f60 commit a0665feac457c47545d62248067e2e32d6c45a25 @markstory markstory committed Oct 26, 2012
View
15 lib/Cake/Model/Model.php
@@ -2613,9 +2613,12 @@ public function hasAny($conditions = null) {
*
* Note: find(list) + database views have issues with MySQL 5.0. Try upgrading to MySQL 5.1 if you
* have issues with database views.
+ *
+ * Note: find(count) has its own return values.
+ *
* @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 Array of records
+ * @return array Array of records, or Null on failure.
* @link http://book.cakephp.org/2.0/en/models/deleting-data.html#deleteall
*/
public function find($type = 'first', $query = array()) {
@@ -2638,10 +2641,10 @@ public function find($type = 'first', $query = array()) {
if ($type === 'all') {
return $results;
- } else {
- if ($this->findMethods[$type] === true) {
- return $this->{'_find' . ucfirst($type)}('after', $query, $results);
- }
+ }
+
+ if ($this->findMethods[$type] === true) {
+ return $this->{'_find' . ucfirst($type)}('after', $query, $results);
}
}
@@ -2707,7 +2710,7 @@ protected function _findFirst($state, $query, $results = array()) {
return $query;
} elseif ($state === 'after') {
if (empty($results[0])) {
- return false;
+ return array();
}
return $results[0];
}
View
2 lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php
@@ -422,7 +422,7 @@ public function testMissingTranslation() {
$TestModel = new TranslatedItem();
$TestModel->locale = 'rus';
$result = $TestModel->read(null, 1);
- $this->assertFalse($result);
+ $this->assertEquals(array(), $result);
$TestModel->locale = array('rus');
$result = $TestModel->read(null, 1);
View
14 lib/Cake/Test/Case/Model/ModelDeleteTest.php
@@ -107,7 +107,7 @@ public function testDeleteHabtmReferenceWithConditions() {
$result = $Portfolio->find('first', array(
'conditions' => array('Portfolio.id' => 1)
));
- $this->assertFalse($result);
+ $this->assertEquals(array(), $result);
$result = $Portfolio->ItemsPortfolio->find('all', array(
'conditions' => array('ItemsPortfolio.portfolio_id' => 1)
@@ -195,7 +195,7 @@ public function testDelete() {
$this->assertTrue($result);
$result = $TestModel->read(null, 2);
- $this->assertFalse($result);
+ $this->assertEquals(array(), $result);
$TestModel->recursive = -1;
$result = $TestModel->find('all', array(
@@ -216,7 +216,7 @@ public function testDelete() {
$this->assertTrue($result);
$result = $TestModel->read(null, 3);
- $this->assertFalse($result);
+ $this->assertEquals(array(), $result);
$TestModel->recursive = -1;
$result = $TestModel->find('all', array(
@@ -448,16 +448,16 @@ public function testRecursiveDel() {
$TestModel->recursive = 2;
$result = $TestModel->read(null, 2);
- $this->assertFalse($result);
+ $this->assertEquals(array(), $result);
$result = $TestModel->Comment->read(null, 5);
- $this->assertFalse($result);
+ $this->assertEquals(array(), $result);
$result = $TestModel->Comment->read(null, 6);
- $this->assertFalse($result);
+ $this->assertEquals(array(), $result);
$result = $TestModel->Comment->Attachment->read(null, 1);
- $this->assertFalse($result);
+ $this->assertEquals(array(), $result);
$result = $TestModel->find('count');
$this->assertEquals(2, $result);

0 comments on commit a0665fe

Please sign in to comment.