Browse Source

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
Mark Story 11 years ago
parent
commit
7ad3ee672b

+ 26 - 1
src/Datasource/EntityTrait.php

@@ -526,7 +526,10 @@ trait EntityTrait
 
     /**
      * 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
@@ -536,6 +539,28 @@ trait EntityTrait
         $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) {
+            $original = $this->getOriginal($property);
             if ($original !== null && $original !== $this->get($property)) {
                 $result[$property] = $original;
             }

+ 1 - 1
src/ORM/Behavior/CounterCacheBehavior.php

@@ -138,7 +138,7 @@ class CounterCacheBehavior extends Behavior
         $countConditions = $entity->extract($foreignKeys);
         $updateConditions = array_combine($primaryKeys, $countConditions);
 
-        $countOriginalConditions = $entity->extractOriginal($foreignKeys);
+        $countOriginalConditions = $entity->extractOriginalDirty($foreignKeys);
         if ($countOriginalConditions !== []) {
             $updateOriginalConditions = array_combine($primaryKeys, $countOriginalConditions);
         }

+ 28 - 0
tests/TestCase/ORM/EntityTest.php

@@ -72,6 +72,34 @@ class EntityTest extends TestCase
     }
 
     /**
+     * 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
      *
      * @return void