Browse Source

Allow camelBacked virtualProperties to be accessed.

This fixes a regression introduced in while optimizing entity accessors.

Refs #8246
mark_story 10 years ago
parent
commit
460fbf2609
2 changed files with 20 additions and 1 deletions
  1. 3 1
      src/Datasource/EntityTrait.php
  2. 17 0
      tests/TestCase/ORM/EntityTest.php

+ 3 - 1
src/Datasource/EntityTrait.php

@@ -535,7 +535,9 @@ trait EntityTrait
             if ($method[0] !== '_' || ($prefix !== 'get' && $prefix !== 'set')) {
                 continue;
             }
-            $field = Inflector::underscore(substr($method, 4));
+            $field = lcfirst(substr($method, 4));
+            $snakeField = Inflector::underscore($field);
+            static::$_accessors[$class][$prefix][$snakeField] = $method;
             static::$_accessors[$class][$prefix][$field] = $method;
         }
 

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

@@ -313,6 +313,23 @@ class EntityTest extends TestCase
     }
 
     /**
+     * Test getting camelcased virtual fields.
+     *
+     * @return void
+     */
+    public function testGetCamelCasedProperties()
+    {
+        $entity = $this->getMock('\Cake\ORM\Entity', ['_getListIdName']);
+        $entity->expects($this->any())->method('_getListIdName')
+            ->will($this->returnCallback(function ($name) {
+                return 'A name';
+            }));
+        $entity->virtualProperties(['ListIdName']);
+        $this->assertSame('A name', $entity->list_id_name, 'underscored virtual field should be accessible');
+        $this->assertSame('A name', $entity->listIdName, 'Camelbacked virtual field should be accessible');
+    }
+
+    /**
      * Test magic property setting with no custom setter
      *
      * @return void