Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split combined getter/setter EntityInterface::isNew(). #13455

Merged
merged 3 commits into from
Aug 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/Datasource/EntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,20 @@ public function extract(array $fields, bool $onlyDirty = false): array;
public function clean(): void;

/**
* Returns whether or not this entity has already been persisted.
* This method can return null in the case there is no prior information on
* the status of this entity.
* Set the status of this entity.
*
* Using `true` means that the entity has not been persisted in the database,
* `false` indicates that the entity has been persisted.
*
* If called with a boolean, this method will set the status of this instance.
* Using `true` means that the instance has not been persisted in the database, `false`
* that it already is.
* @param bool $new Indicate whether or not this entity has been persisted.
* @return $this
*/
public function setNew(bool $new);
garas marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns whether or not this entity has already been persisted.
*
* @param bool|null $new Indicate whether or not this instance has been 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(?bool $new = null): bool;
public function isNew(): bool;
}
41 changes: 25 additions & 16 deletions src/Datasource/EntityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -788,32 +788,41 @@ public function clean(): void
}

/**
* Returns whether or not this entity has already been persisted.
* This method can return null in the case there is no prior information on
* the status of this entity.
* Set the status of this entity.
*
* If called with a boolean it will set the known status of this instance,
* true means that the instance is not yet persisted in the database, false
* that it already is.
* Using `true` means that the entity has not been persisted in the database,
* `false` that it already is.
*
* @param bool|null $new True if it is known this instance was not yet persisted
* @return bool Whether or not the entity has been persisted.
* @param bool $new Indicate whether or not this entity has been persisted.
* @return $this
*/
public function isNew(?bool $new = null): bool
public function setNew(bool $new)
{
if ($new === null) {
return $this->_new;
}

$new = (bool)$new;

if ($new) {
foreach ($this->_fields as $k => $p) {
$this->_dirty[$k] = true;
}
}

return $this->_new = $new;
$this->_new = $new;

return $this;
}

/**
* Returns whether or not this entity has already been persisted.
*
* @return bool Whether or not the entity has been persisted.
*/
public function isNew(): bool
{
if (func_num_args()) {
deprecationWarning('Using isNew() as setter is deprecated. Use setNew() instead.');

$this->setNew(func_get_arg(0));
}

return $this->_new;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Association/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ protected function _saveLinks(EntityInterface $sourceEntity, array $targetEntiti
// as new, we let save() sort out whether or not we have a new link
// or if we are updating an existing link.
if ($changedKeys) {
$joint->isNew(true);
$joint->setNew(true);
$joint->unset($junction->getPrimaryKey())
->set(array_merge($sourceKeys, $targetKeys), ['guard' => false]);
}
Expand Down
4 changes: 2 additions & 2 deletions src/ORM/Behavior/Translate/EavStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,11 @@ protected function bundleTranslatedFields($entity)
foreach ($find as $i => $translation) {
if (!empty($results[$i])) {
$contents[$i]->set('id', $results[$i], ['setter' => false]);
$contents[$i]->isNew(false);
$contents[$i]->setNew(false);
} else {
$translation['model'] = $this->_config['referenceName'];
$contents[$i]->set($translation, ['setter' => false, 'guard' => false]);
$contents[$i]->isNew(true);
$contents[$i]->setNew(true);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function __construct(array $properties = [], array $options = [])
}

if ($options['markNew'] !== null) {
$this->isNew($options['markNew']);
$this->setNew($options['markNew']);
}

if (!empty($properties) && $options['markClean'] && !$options['useSetters']) {
Expand Down
10 changes: 5 additions & 5 deletions src/ORM/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -1765,7 +1765,7 @@ public function save(EntityInterface $entity, $options = [])
}
if ($options['atomic'] || $options['_primary']) {
$entity->clean();
$entity->isNew(false);
$entity->setNew(false);
$entity->setSource($this->getRegistryAlias());
}
}
Expand Down Expand Up @@ -1813,7 +1813,7 @@ protected function _processSave(EntityInterface $entity, ArrayObject $options)
foreach ($entity->extract($primaryColumns) as $k => $v) {
$conditions["$alias.$k"] = $v;
}
$entity->isNew(!$this->exists($conditions));
$entity->setNew(!$this->exists($conditions));
}

$mode = $entity->isNew() ? RulesChecker::CREATE : RulesChecker::UPDATE;
Expand Down Expand Up @@ -1854,7 +1854,7 @@ protected function _processSave(EntityInterface $entity, ArrayObject $options)

if (!$success && $isNew) {
$entity->unset($this->getPrimaryKey());
$entity->isNew(true);
$entity->setNew(true);
}

return $success ? $entity : false;
Expand Down Expand Up @@ -1891,7 +1891,7 @@ protected function _onSaveSuccess(EntityInterface $entity, ArrayObject $options)

if (!$options['atomic'] && !$options['_primary']) {
$entity->clean();
$entity->isNew(false);
$entity->setNew(false);
$entity->setSource($this->getRegistryAlias());
}

Expand Down Expand Up @@ -2096,7 +2096,7 @@ protected function _saveMany(iterable $entities, $options = []): iterable
foreach ($entities as $key => $entity) {
if (isset($isNew[$key]) && $isNew[$key]) {
$entity->unset($this->getPrimaryKey());
$entity->isNew(true);
$entity->setNew(true);
}
}
};
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase/ORM/Behavior/TimestampBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function testCreatedNotNew()

$event = new Event('Model.beforeSave');
$entity = new Entity(['name' => 'Foo']);
$entity->isNew(false);
$entity->setNew(false);

$return = $this->Behavior->handleEvent($event, $entity);
$this->assertTrue($return, 'Handle Event is expected to always return true');
Expand All @@ -165,7 +165,7 @@ public function testModifiedAbsent()

$event = new Event('Model.beforeSave');
$entity = new Entity(['name' => 'Foo']);
$entity->isNew(false);
$entity->setNew(false);

$return = $this->Behavior->handleEvent($event, $entity);
$this->assertTrue($return, 'Handle Event is expected to always return true');
Expand All @@ -190,7 +190,7 @@ public function testModifiedPresent()
$existingValue = new \DateTime('2011-11-11');
$entity = new Entity(['name' => 'Foo', 'modified' => $existingValue]);
$entity->clean();
$entity->isNew(false);
$entity->setNew(false);

$return = $this->Behavior->handleEvent($event, $entity);
$this->assertTrue($return, 'Handle Event is expected to always return true');
Expand Down
12 changes: 6 additions & 6 deletions tests/TestCase/ORM/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -938,10 +938,10 @@ public function testIsNew()
$entity = new Entity($data);
$this->assertTrue($entity->isNew());

$entity->isNew(true);
$entity->setNew(true);
$this->assertTrue($entity->isNew());

$entity->isNew(false);
$entity->setNew(false);
$this->assertFalse($entity->isNew());
}

Expand Down Expand Up @@ -975,17 +975,17 @@ public function testConstructorWithClean()
public function testConstructorWithMarkNew()
{
$entity = $this->getMockBuilder('Cake\ORM\Entity')
->setMethods(['isNew', 'clean'])
->setMethods(['setNew', 'clean'])
->disableOriginalConstructor()
->getMock();
$entity->expects($this->never())->method('clean');
$entity->__construct(['a' => 'b', 'c' => 'd']);

$entity = $this->getMockBuilder('Cake\ORM\Entity')
->setMethods(['isNew'])
->setMethods(['setNew'])
->disableOriginalConstructor()
->getMock();
$entity->expects($this->once())->method('isNew');
$entity->expects($this->once())->method('setNew');
$entity->__construct(['a' => 'b', 'c' => 'd'], ['markNew' => true]);
}

Expand Down Expand Up @@ -1642,7 +1642,7 @@ public function testIsDirtyFromClone()
$this->assertFalse($entity->isDirty());

$cloned = clone $entity;
$cloned->isNew(true);
$cloned->setNew(true);

$this->assertTrue($cloned->isDirty());
$this->assertTrue($cloned->isDirty('a'));
Expand Down
16 changes: 8 additions & 8 deletions tests/TestCase/ORM/MarshallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ public function testMergeSimple()
'body' => 'My Content',
]);
$entity->setAccess('*', true);
$entity->isNew(false);
$entity->setNew(false);
$entity->clean();
$result = $marshall->merge($entity, $data, []);

Expand Down Expand Up @@ -1291,7 +1291,7 @@ public function testMergeAccessibleFields()
'body' => 'My Content',
]);
$entity->setAccess('*', false);
$entity->isNew(false);
$entity->setNew(false);
$entity->clean();
$result = $marshall->merge($entity, $data, ['accessibleFields' => ['body' => true]]);

Expand Down Expand Up @@ -1349,7 +1349,7 @@ public function testMergeUnchangedNullValue()
'body' => null,
]);
$entity->setAccess('*', true);
$entity->isNew(false);
$entity->setNew(false);
$entity->clean();
$result = $marshall->merge($entity, $data, []);

Expand All @@ -1375,7 +1375,7 @@ public function testMergeWhitelist()
]);
$entity->setAccess('*', false);
$entity->setAccess('author_id', true);
$entity->isNew(false);
$entity->setNew(false);
$entity->clean();

$result = $marshall->merge($entity, $data, []);
Expand Down Expand Up @@ -2584,7 +2584,7 @@ public function testMergeWithFields()
'author_id' => 2,
]);
$entity->setAccess('*', false);
$entity->isNew(false);
$entity->setNew(false);
$entity->clean();
$result = $marshall->merge($entity, $data, ['fields' => ['title', 'body']]);

Expand Down Expand Up @@ -3017,7 +3017,7 @@ public function testMergeWithValidation()
$this->assertEmpty($entity->getInvalid());

$entity->setAccess('*', true);
$entity->isNew(false);
$entity->setNew(false);
$entity->clean();

$this->articles->getValidator()
Expand Down Expand Up @@ -3059,7 +3059,7 @@ public function testMergeWithCreate()
'author_id' => 1,
]);
$entity->setAccess('*', true);
$entity->isNew(true);
$entity->setNew(true);
$entity->clean();

$this->articles->getValidator()
Expand All @@ -3073,7 +3073,7 @@ public function testMergeWithCreate()
$this->assertEmpty($result->getError('thing'));

$entity->clean();
$entity->isNew(false);
$entity->setNew(false);
$result = $marshall->merge($entity, $data, []);
$this->assertNotEmpty($result->getError('author_id'));
$this->assertNotEmpty($result->getError('thing'));
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/ORM/ResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function testIteratorAfterSerializationHydrated()
// Use a loop to test Iterator implementation
foreach ($results as $i => $row) {
$expected = new Entity($this->fixtureData[$i]);
$expected->isNew(false);
$expected->setNew(false);
$expected->setSource($this->table->getAlias());
$expected->clean();
$this->assertEquals($expected, $row, "Row $i does not match");
Expand Down
18 changes: 9 additions & 9 deletions tests/TestCase/ORM/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3017,7 +3017,7 @@ public function testDeleteCallbacks()
}));

$table = $this->getTableLocator()->get('users', ['eventManager' => $mock]);
$entity->isNew(false);
$entity->setNew(false);
$table->delete($entity, ['checkRules' => false]);
}

Expand Down Expand Up @@ -3103,7 +3103,7 @@ public function testDeleteBeforeDeleteAbort()
}));

$table = $this->getTableLocator()->get('users', ['eventManager' => $mock]);
$entity->isNew(false);
$entity->setNew(false);
$result = $table->delete($entity, ['checkRules' => false]);
$this->assertFalse($result);
}
Expand All @@ -3129,7 +3129,7 @@ public function testDeleteBeforeDeleteReturnResult()
}));

$table = $this->getTableLocator()->get('users', ['eventManager' => $mock]);
$entity->isNew(false);
$entity->setNew(false);
$result = $table->delete($entity, ['checkRules' => false]);
$this->assertTrue($result);
}
Expand All @@ -3151,7 +3151,7 @@ public function testDeleteIsNew()
$table->expects($this->never())
->method('query');

$entity->isNew(true);
$entity->setNew(true);
$result = $table->delete($entity);
$this->assertFalse($result);
}
Expand Down Expand Up @@ -4034,10 +4034,10 @@ public function testSaveDeepAssociationOptions()
],
]),
]);
$entity->isNew(true);
$entity->author->isNew(true);
$entity->author->supervisor->isNew(true);
$entity->author->tags[0]->isNew(true);
$entity->setNew(true);
$entity->author->setNew(true);
$entity->author->supervisor->setNew(true);
$entity->author->tags[0]->setNew(true);

$articles->expects($this->once())
->method('_insert')
Expand Down Expand Up @@ -6192,7 +6192,7 @@ public function testSaveWithClonedEntity()

$cloned = clone $article;
$cloned->unset('id');
$cloned->isNew(true);
$cloned->setNew(true);
$this->assertSame($cloned, $table->save($cloned));
$this->assertEquals(
$article->extract(['title', 'author_id']),
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/View/Form/EntityContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ public function testIsCreateSingle()
]);
$this->assertTrue($context->isCreate());

$row->isNew(false);
$row->setNew(false);
$this->assertFalse($context->isCreate());

$row->isNew(true);
$row->setNew(true);
$this->assertTrue($context->isCreate());
}

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/View/Helper/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4389,7 +4389,7 @@ public function testTextDefaultValue()
$this->assertHtml($expected, $result);

// Default value from schema is used only for new entities.
$entity->isNew(false);
$entity->setNew(false);
$result = $this->Form->text('title');
$expected = ['input' => ['type' => 'text', 'name' => 'title']];
$this->assertHtml($expected, $result);
Expand Down