Browse Source

Add a method to get all original values from an entity

Florian Krämer 10 years ago
parent
commit
1996fe9e39
2 changed files with 31 additions and 0 deletions
  1. 10 0
      src/Datasource/EntityTrait.php
  2. 21 0
      tests/TestCase/ORM/EntityTest.php

+ 10 - 0
src/Datasource/EntityTrait.php

@@ -316,6 +316,16 @@ trait EntityTrait
     }
 
     /**
+     * Gets all original values of the entity.
+     *
+     * @return array
+     */
+    public function getOriginalValues()
+    {
+        return $this->_original;
+    }
+
+    /**
      * Returns whether this entity contains a property named $property
      * that contains a non-null value.
      *

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

@@ -132,6 +132,27 @@ class EntityTest extends TestCase
     }
 
     /**
+     * Test that all original values are returned properly
+     *
+     * @return void
+     */
+    public function testExtractOriginalValues()
+    {
+        $entity = new Entity([
+            'id' => 1,
+            'title' => 'original',
+            'body' => 'no',
+            'null' => null,
+        ], ['markNew' => true]);
+        $entity->set('body', 'updated body');
+        $result = $entity->getOriginalValues(['id', 'title', 'body', 'null']);
+        $expected = [
+            'body' => 'no'
+        ];
+        $this->assertEquals($expected, $result);
+    }
+
+    /**
      * Tests setting a single property using a setter function
      *
      * @return void