Merge pull request #917 from bar/master-find

Make Model::find('first') always return an array.
This commit is contained in:
Mark Story 2012-10-26 07:00:48 -07:00
commit a0665feac4
3 changed files with 17 additions and 14 deletions

View file

@ -2613,9 +2613,12 @@ class Model extends Object implements CakeEventListener {
* *
* Note: find(list) + database views have issues with MySQL 5.0. Try upgrading to MySQL 5.1 if you * Note: find(list) + database views have issues with MySQL 5.0. Try upgrading to MySQL 5.1 if you
* have issues with database views. * 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 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)
* @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 * @link http://book.cakephp.org/2.0/en/models/deleting-data.html#deleteall
*/ */
public function find($type = 'first', $query = array()) { public function find($type = 'first', $query = array()) {
@ -2638,10 +2641,10 @@ class Model extends Object implements CakeEventListener {
if ($type === 'all') { if ($type === 'all') {
return $results; 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 @@ class Model extends Object implements CakeEventListener {
return $query; return $query;
} elseif ($state === 'after') { } elseif ($state === 'after') {
if (empty($results[0])) { if (empty($results[0])) {
return false; return array();
} }
return $results[0]; return $results[0];
} }

View file

@ -422,7 +422,7 @@ class TranslateBehaviorTest extends CakeTestCase {
$TestModel = new TranslatedItem(); $TestModel = new TranslatedItem();
$TestModel->locale = 'rus'; $TestModel->locale = 'rus';
$result = $TestModel->read(null, 1); $result = $TestModel->read(null, 1);
$this->assertFalse($result); $this->assertEquals(array(), $result);
$TestModel->locale = array('rus'); $TestModel->locale = array('rus');
$result = $TestModel->read(null, 1); $result = $TestModel->read(null, 1);

View file

@ -107,7 +107,7 @@ class ModelDeleteTest extends BaseModelTest {
$result = $Portfolio->find('first', array( $result = $Portfolio->find('first', array(
'conditions' => array('Portfolio.id' => 1) 'conditions' => array('Portfolio.id' => 1)
)); ));
$this->assertFalse($result); $this->assertEquals(array(), $result);
$result = $Portfolio->ItemsPortfolio->find('all', array( $result = $Portfolio->ItemsPortfolio->find('all', array(
'conditions' => array('ItemsPortfolio.portfolio_id' => 1) 'conditions' => array('ItemsPortfolio.portfolio_id' => 1)
@ -195,7 +195,7 @@ class ModelDeleteTest extends BaseModelTest {
$this->assertTrue($result); $this->assertTrue($result);
$result = $TestModel->read(null, 2); $result = $TestModel->read(null, 2);
$this->assertFalse($result); $this->assertEquals(array(), $result);
$TestModel->recursive = -1; $TestModel->recursive = -1;
$result = $TestModel->find('all', array( $result = $TestModel->find('all', array(
@ -216,7 +216,7 @@ class ModelDeleteTest extends BaseModelTest {
$this->assertTrue($result); $this->assertTrue($result);
$result = $TestModel->read(null, 3); $result = $TestModel->read(null, 3);
$this->assertFalse($result); $this->assertEquals(array(), $result);
$TestModel->recursive = -1; $TestModel->recursive = -1;
$result = $TestModel->find('all', array( $result = $TestModel->find('all', array(
@ -448,16 +448,16 @@ class ModelDeleteTest extends BaseModelTest {
$TestModel->recursive = 2; $TestModel->recursive = 2;
$result = $TestModel->read(null, 2); $result = $TestModel->read(null, 2);
$this->assertFalse($result); $this->assertEquals(array(), $result);
$result = $TestModel->Comment->read(null, 5); $result = $TestModel->Comment->read(null, 5);
$this->assertFalse($result); $this->assertEquals(array(), $result);
$result = $TestModel->Comment->read(null, 6); $result = $TestModel->Comment->read(null, 6);
$this->assertFalse($result); $this->assertEquals(array(), $result);
$result = $TestModel->Comment->Attachment->read(null, 1); $result = $TestModel->Comment->Attachment->read(null, 1);
$this->assertFalse($result); $this->assertEquals(array(), $result);
$result = $TestModel->find('count'); $result = $TestModel->find('count');
$this->assertEquals(2, $result); $this->assertEquals(2, $result);