mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-02-12 06:56:24 +00:00
Refactoring Model::exists() to be independent of Model::$useTable. Fixes #199.
Model::exists() now makes no check whatsoever on the value of Model::$useTable. This means that, as with a database-backed dbo, Model::exists() will call Model::find('count') (which in turn calls DataSource::read()) to determine if the record identified by Model::$id already exists in the datasource.
This commit is contained in:
parent
199a14f3be
commit
adb0c809a0
2 changed files with 30 additions and 2 deletions
|
@ -2006,11 +2006,15 @@ class Model extends Overloadable {
|
||||||
/**
|
/**
|
||||||
* Returns true if a record with the currently set ID exists.
|
* Returns true if a record with the currently set ID exists.
|
||||||
*
|
*
|
||||||
|
* Internally calls Model::getID() to obtain the current record ID to verify,
|
||||||
|
* and then performs a Model::find('count') on the currently configured datasource
|
||||||
|
* to ascertain the existence of the record in persistent storage.
|
||||||
|
*
|
||||||
* @return boolean True if such a record exists
|
* @return boolean True if such a record exists
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
function exists() {
|
function exists() {
|
||||||
if ($this->getID() === false || $this->useTable === false) {
|
if ($this->getID() === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$conditions = array($this->alias . '.' . $this->primaryKey => $this->getID());
|
$conditions = array($this->alias . '.' . $this->primaryKey => $this->getID());
|
||||||
|
|
|
@ -876,7 +876,7 @@ class ModelIntegrationTest extends BaseModelTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ensure that __exists is reset on create
|
* ensure that exists() does not persist between method calls reset on create
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
@ -897,6 +897,30 @@ class ModelIntegrationTest extends BaseModelTest {
|
||||||
$this->assertEqual($result['Article']['title'], 'Staying alive');
|
$this->assertEqual($result['Article']['title'], 'Staying alive');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* testUseTableFalseExistsCheck method
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function testUseTableFalseExistsCheck() {
|
||||||
|
$this->loadFixtures('Article');
|
||||||
|
$Article =& new Article();
|
||||||
|
$Article->id = 1337;
|
||||||
|
$result = $Article->exists();
|
||||||
|
$this->assertFalse($result);
|
||||||
|
|
||||||
|
$Article->useTable = false;
|
||||||
|
$Article->id = null;
|
||||||
|
$result = $Article->exists();
|
||||||
|
$this->assertFalse($result);
|
||||||
|
|
||||||
|
// An article with primary key of '1' has been loaded by the fixtures.
|
||||||
|
$Article->useTable = false;
|
||||||
|
$Article->id = 1;
|
||||||
|
$result = $Article->exists();
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* testPluginAssociations method
|
* testPluginAssociations method
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Reference in a new issue