Skip to content
Permalink
Browse files

Make extractOriginal() consistent with getOriginal().

Make the two methods consistent. Introduce a new method for getting
the original value of properties that have been modified.

Refs #6467
  • Loading branch information...
markstory committed May 3, 2015
1 parent 7c8c540 commit 7ad3ee672b72d1adbd94c3ba23859b7e6beb7f41
Showing with 55 additions and 2 deletions.
  1. +26 −1 src/Datasource/EntityTrait.php
  2. +1 −1 src/ORM/Behavior/CounterCacheBehavior.php
  3. +28 −0 tests/TestCase/ORM/EntityTest.php
@@ -526,12 +526,37 @@ public function extract(array $properties, $onlyDirty = false)
/**
* Returns an array with the requested original properties
* stored in this entity, indexed by property name
* stored in this entity, indexed by property name.
*
* Properties that have not changed will be included in the
* return of this method.
*
* @param array $properties List of properties to be returned
* @return array
*/
public function extractOriginal(array $properties)
{
$result = [];
foreach ($properties as $property) {
$original = $this->getOriginal($property);
if ($original !== null) {
$result[$property] = $original;
}
}
return $result;
}
/**
* Returns an array with only the original properties
* stored in this entity, indexed by property name.
*
* This method will only return properties that have been modified.
* Unchanged properties will be omited.
*
* @param array $properties List of properties to be returned
* @return array
*/
public function extractOriginalDirty(array $properties)
{
$result = [];
foreach ($properties as $property) {
@@ -138,7 +138,7 @@ protected function _processAssociation(Event $event, Entity $entity, Association
$countConditions = $entity->extract($foreignKeys);
$updateConditions = array_combine($primaryKeys, $countConditions);
$countOriginalConditions = $entity->extractOriginal($foreignKeys);
$countOriginalConditions = $entity->extractOriginalDirty($foreignKeys);
if ($countOriginalConditions !== []) {
$updateOriginalConditions = array_combine($primaryKeys, $countOriginalConditions);
}
@@ -71,6 +71,34 @@ public function testSetMultiplePropertiesNoSetters()
$this->assertEquals(1, $entity->getOriginal('id'));
}
/**
* Test extractOriginal()
*
* @return void
*/
public function testExtractOriginal()
{
$entity = new Entity([
'id' => 1,
'title' => 'original',
'body' => 'no'
], ['markNew' => true]);
$entity->set('body', 'updated body');
$result = $entity->extractOriginal(['id', 'title', 'body']);
$expected = [
'id' => 1,
'title' => 'original',
'body' => 'no'
];
$this->assertEquals($expected, $result);
$result = $entity->extractOriginalDirty(['id', 'title', 'body']);
$expected = [
'body' => 'no',
];
$this->assertEquals($expected, $result);
}
/**
* Tests setting a single property using a setter function
*

0 comments on commit 7ad3ee6

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