Skip to content
Permalink
Browse files

Make Entity::isNew() only support true/false

Having an indeterminate state complicates life. Having only true/false
makes interacting with the ORM a bit simpler. Table::save() will still
determine if an entity is truely new when saving and the primary key is
present. Delete operations assume you know what you are doing, and don't
try to help as destroying data is generally more dangerous/difficult to
recover vs. having too much data.

Refs #4050
  • Loading branch information...
markstory committed Jul 24, 2014
1 parent 63215b0 commit a30633b85f1965f51adb1150ca41d278cf268474
@@ -72,11 +72,12 @@ trait EntityTrait {
/**
* Indicates whether or not this entity is yet to be persisted.
* A null value indicates an unknown persistence status
* Entities default to assuming they are new. You can use Table::persisted()
* to set the new flag on an entity based on records in the database.
*
* @var bool
*/
protected $_new = null;
protected $_new = true;
/**
* List of errors per field as stored in this object
@@ -514,8 +515,7 @@ public function clean() {
* that it already is.
*
* @param bool $new true if it is known this instance was persisted
* @return bool if it is known whether the entity was already persisted
* null otherwise
* @return bool Whether or not the entity has been persisted.
*/
public function isNew($new = null) {
if ($new === null) {
@@ -128,7 +128,10 @@ public function write($id, $data) {
* @return bool True for successful delete, false otherwise.
*/
public function destroy($id) {
return $this->_table->delete(new Entity([$this->_table->primaryKey() => $id]));
return $this->_table->delete(new Entity(
[$this->_table->primaryKey() => $id],
['markNew' => false]
));
}
/**
@@ -786,13 +786,13 @@ protected function _diffLinks($existing, $jointEntities, $targetEntities) {
* @throws \InvalidArgumentException
*/
protected function _checkPersistenceStatus($sourceEntity, array $targetEntities) {
if ($sourceEntity->isNew() !== false) {
if ($sourceEntity->isNew()) {
$error = 'Source entity needs to be persisted before proceeding';
throw new \InvalidArgumentException($error);
}
foreach ($targetEntities as $entity) {
if ($entity->isNew() !== false) {
if ($entity->isNew()) {
$error = 'Cannot link not persisted entities';
throw new \InvalidArgumentException($error);
}
@@ -1084,8 +1084,7 @@ public function exists($conditions) {
*
* This method will determine whether the passed entity needs to be
* inserted or updated in the database. It does that by checking the `isNew`
* method on the entity, if no information can be found there, it will go
* directly to the database to check the entity's status.
* method on the entity.
*
* ### Saving on associated tables
*
@@ -1147,7 +1146,7 @@ public function save(EntityInterface $entity, $options = []) {
protected function _processSave($entity, $options) {
$primary = $entity->extract((array)$this->primaryKey());
if ($primary && $entity->isNew() === null) {
if ($primary && $entity->isNew()) {
$alias = $this->alias();
$keys = array_keys($primary);
foreach ($keys as &$pk) {
@@ -1156,10 +1155,6 @@ protected function _processSave($entity, $options) {
$entity->isNew(!$this->exists(array_combine($keys, $primary)));
}
if ($entity->isNew() === null) {
$entity->isNew(true);
}
$associated = $options['associated'];
$options['associated'] = [];
$validate = $options['validate'];
@@ -571,9 +571,14 @@ public function testIsNew() {
'title' => 'Foo',
'author_id' => 3
]);
$this->assertNull($entity->isNew());
$this->assertTrue($entity->isNew());
$entity->isNew(true);
$this->assertTrue($entity->isNew());
$entity->isNew('derpy');
$this->assertTrue($entity->isNew());
$entity->isNew(false);
$this->assertFalse($entity->isNew());
}
@@ -1042,7 +1047,7 @@ public function testDebugInfo() {
$entity->source('foos');
$result = $entity->__debugInfo();
$expected = [
'new' => null,
'new' => true,
'accessible' => ['*' => true, 'name' => true],
'properties' => ['foo' => 'bar'],
'dirty' => ['foo' => true],
@@ -110,7 +110,7 @@ public function testOneSimple() {
$this->assertInstanceOf('Cake\ORM\Entity', $result);
$this->assertEquals($data, $result->toArray());
$this->assertTrue($result->dirty(), 'Should be a dirty entity.');
$this->assertNull($result->isNew(), 'Should be detached');
$this->assertTrue($result->isNew(), 'Should be new');
$this->assertEquals('Articles', $result->source());
}
@@ -206,7 +206,7 @@ public function testOneWithAdditionalName() {
$this->assertInstanceOf('Cake\ORM\Entity', $result);
$this->assertTrue($result->dirty(), 'Should be a dirty entity.');
$this->assertNull($result->isNew(), 'Should be detached');
$this->assertTrue($result->isNew(), 'Should be new');
$this->assertEquals($data['Articles']['title'], $result->title);
$this->assertEquals($data['Articles']['user']['username'], $result->user->username);
}
@@ -645,7 +645,7 @@ public function testMergeCreateAssociation() {
$this->assertEquals('mark', $entity->user->username);
$this->assertEquals('not a secret', $entity->user->password);
$this->assertTrue($entity->dirty('user'));
$this->assertNull($entity->user->isNew());
$this->assertTrue($entity->user->isNew());
}
/**
@@ -2515,8 +2515,8 @@ public function testSaveHasManyWithErrorsAtomic() {
$this->assertFalse($table->save($entity));
$this->assertTrue($entity->isNew());
$this->assertNull($entity->articles[0]->isNew());
$this->assertNull($entity->articles[1]->isNew());
$this->assertTrue($entity->articles[0]->isNew());
$this->assertTrue($entity->articles[1]->isNew());
$this->assertNull($entity->articles[0]->id);
$this->assertNull($entity->articles[1]->id);
$this->assertNull($entity->articles[0]->author_id);
@@ -2741,8 +2741,8 @@ public function testSaveBelongsToWithValidationErrorAtomic() {
$this->assertFalse($table->save($entity));
$this->assertTrue($entity->isNew());
$this->assertNull($entity->tags[0]->isNew());
$this->assertNull($entity->tags[1]->isNew());
$this->assertTrue($entity->tags[0]->isNew());
$this->assertTrue($entity->tags[1]->isNew());
$this->assertNull($entity->tags[0]->id);
$this->assertNull($entity->tags[1]->id);
$this->assertNull($entity->tags[0]->_joinData);
@@ -2817,8 +2817,8 @@ public function testSaveBelongsToWithValidationErrorInJointEntity() {
$this->assertFalse($table->save($entity));
$this->assertTrue($entity->isNew());
$this->assertNull($entity->tags[0]->isNew());
$this->assertNull($entity->tags[1]->isNew());
$this->assertTrue($entity->tags[0]->isNew());
$this->assertTrue($entity->tags[1]->isNew());
$this->assertNull($entity->tags[0]->id);
$this->assertNull($entity->tags[1]->id);
$this->assertNull($entity->tags[0]->_joinData);
@@ -3093,7 +3093,7 @@ public function testUnlinkBelongsToManyPassingJoint() {
$tags[1]->_joinData = new \Cake\ORM\Entity([
'article_id' => 1,
'tag_id' => 2
]);
], $options);
$table->association('tags')->unlink($article, $tags);
$left = $table->find('all')->where(['id' => 1])->contain(['tags'])->first();

0 comments on commit a30633b

Please sign in to comment.
You can’t perform that action at this time.