ソースを参照

Merge pull request #12285 from ndm2/fix-association-schema-default-lookups

Make entity context recognize schema defaults of associations.
Mark Story 7 年 前
コミット
91cba50d07

+ 6 - 6
src/View/Form/EntityContext.php

@@ -249,7 +249,7 @@ class EntityContext implements ContextInterface
         }
 
         if ($entity instanceof EntityInterface) {
-            $part = array_pop($parts);
+            $part = end($parts);
             $val = $entity->get($part);
             if ($val !== null) {
                 return $val;
@@ -261,7 +261,7 @@ class EntityContext implements ContextInterface
                 return $options['default'];
             }
 
-            return $this->_schemaDefault($part, $entity);
+            return $this->_schemaDefault($parts);
         }
         if (is_array($entity) || $entity instanceof ArrayAccess) {
             $key = array_pop($parts);
@@ -275,16 +275,16 @@ class EntityContext implements ContextInterface
     /**
      * Get default value from table schema for given entity field.
      *
-     * @param string $field Field name.
-     * @param \Cake\Datasource\EntityInterface $entity The entity.
+     * @param array $parts Each one of the parts in a path for a field name
      * @return mixed
      */
-    protected function _schemaDefault($field, $entity)
+    protected function _schemaDefault($parts)
     {
-        $table = $this->_getTable($entity);
+        $table = $this->_getTable($parts);
         if ($table === false) {
             return null;
         }
+        $field = end($parts);
         $defaults = $table->getSchema()->defaultValues();
         if (!array_key_exists($field, $defaults)) {
             return null;

+ 47 - 0
tests/TestCase/View/Form/EntityContextTest.php

@@ -720,6 +720,53 @@ class EntityContextTest extends TestCase
     }
 
     /**
+     * Test getting association default value from table schema.
+     *
+     * @return void
+     */
+    public function testValAssociatedSchemaDefault()
+    {
+        $table = $this->getTableLocator()->get('Articles');
+        $associatedTable = $table->hasMany('Comments')->getTarget();
+        $column = $associatedTable->getSchema()->getColumn('comment');
+        $associatedTable->getSchema()->addColumn('comment', ['default' => 'default comment'] + $column);
+        $row = $table->newEntity();
+
+        $context = new EntityContext($this->request, [
+            'entity' => $row,
+            'table' => 'Articles',
+        ]);
+        $result = $context->val('comments.0.comment');
+        $this->assertEquals('default comment', $result);
+    }
+
+    /**
+     * Test getting association join table default value from table schema.
+     *
+     * @return void
+     */
+    public function testValAssociatedJoinTableSchemaDefault()
+    {
+        $table = $this->getTableLocator()->get('Articles');
+        $joinTable = $table
+            ->belongsToMany('Tags')
+            ->setThrough('ArticlesTags')
+            ->junction();
+        $joinTable->getSchema()->addColumn('column', [
+            'default' => 'default join table column value',
+            'type' => 'text'
+        ]);
+        $row = $table->newEntity();
+
+        $context = new EntityContext($this->request, [
+            'entity' => $row,
+            'table' => 'Articles',
+        ]);
+        $result = $context->val('tags.0._joinData.column');
+        $this->assertEquals('default join table column value', $result);
+    }
+
+    /**
      * Test validator for boolean fields.
      *
      * @return void