Skip to content

Commit

Permalink
Added ability to pass ID as parameter to Model::exists()
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Feb 24, 2012
1 parent fd92720 commit 4edb378
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
14 changes: 9 additions & 5 deletions lib/Cake/Model/Model.php
Expand Up @@ -2537,19 +2537,23 @@ protected function _collectForeignKeys($type = 'belongsTo') {
} }


/** /**
* Returns true if a record with the currently set ID exists. * Returns true if a record with particular ID exists.
* *
* Internally calls Model::getID() to obtain the current record ID to verify, * If $id is not passed it calls Model::getID() to obtain the current record ID,
* and then performs a Model::find('count') on the currently configured datasource * and then performs a Model::find('count') on the currently configured datasource
* to ascertain the existence of the record in persistent storage. * to ascertain the existence of the record in persistent storage.
* *
* @param mixed $id ID of record to check for existence
* @return boolean True if such a record exists * @return boolean True if such a record exists
*/ */
public function exists() { public function exists($id = null) {
if ($this->getID() === false) { if ($id === null) {
$id = $this->getID();
}
if ($id === false) {
return false; return false;
} }
$conditions = array($this->alias . '.' . $this->primaryKey => $this->getID()); $conditions = array($this->alias . '.' . $this->primaryKey => $id);
$query = array('conditions' => $conditions, 'recursive' => -1, 'callbacks' => false); $query = array('conditions' => $conditions, 'recursive' => -1, 'callbacks' => false);
return ($this->find('count', $query) > 0); return ($this->find('count', $query) > 0);
} }
Expand Down
29 changes: 24 additions & 5 deletions lib/Cake/Test/Case/Model/ModelReadTest.php
Expand Up @@ -24,6 +24,25 @@
*/ */
class ModelReadTest extends BaseModelTest { class ModelReadTest extends BaseModelTest {


/**
* testExists function
* @retun void
*/
public function testExists() {
$this->loadFixtures('User');
$TestModel = new User();

$this->assertTrue($TestModel->exists(1));

$TestModel->id = 2;
$this->assertTrue($TestModel->exists());

$TestModel->delete();
$this->assertFalse($TestModel->exists());

$this->assertFalse($TestModel->exists(2));
}

/** /**
* testFetchingNonUniqueFKJoinTableRecords() * testFetchingNonUniqueFKJoinTableRecords()
* *
Expand Down Expand Up @@ -7784,18 +7803,18 @@ public function testGetVirtualField() {
$this->assertEquals($Post->getVirtualField('other_field'), $Post->virtualFields['other_field']); $this->assertEquals($Post->getVirtualField('other_field'), $Post->virtualFields['other_field']);
$this->assertEquals($Post->getVirtualField('Post.other_field'), $Post->virtualFields['other_field']); $this->assertEquals($Post->getVirtualField('Post.other_field'), $Post->virtualFields['other_field']);
} }


/** /**
* test that checks for error when NOT condition passed in key and a 1 element array value * test that checks for error when NOT condition passed in key and a 1 element array value
* *
* @return void * @return void
*/ */
public function testNotInArrayWithOneValue() { public function testNotInArrayWithOneValue() {
$this->loadFixtures('Article'); $this->loadFixtures('Article');
$Article = new Article(); $Article = new Article();
$Article->recursive = -1; $Article->recursive = -1;

$result = $Article->find( $result = $Article->find(
'all', 'all',
array( array(
Expand All @@ -7804,7 +7823,7 @@ public function testNotInArrayWithOneValue() {
) )
) )
); );

$this->assertTrue(is_array($result) && !empty($result)); $this->assertTrue(is_array($result) && !empty($result));
} }
} }

0 comments on commit 4edb378

Please sign in to comment.