瀏覽代碼

Merge pull request #3128 from pulzarraider/collection_fix_null

Fixed bug in sonata_type_collection that calls entity's remove method with null argument in Symfony 2.3
Andrej Hudec 9 年之前
父節點
當前提交
355eefae00
共有 1 個文件被更改,包括 18 次插入4 次删除
  1. 18 4
      Form/Type/AdminType.php

+ 18 - 4
Form/Type/AdminType.php

@@ -11,6 +11,7 @@
 
 
 namespace Sonata\AdminBundle\Form\Type;
 namespace Sonata\AdminBundle\Form\Type;
 
 
+use Doctrine\Common\Collections\Collection;
 use Sonata\AdminBundle\Form\DataTransformer\ArrayToModelTransformer;
 use Sonata\AdminBundle\Form\DataTransformer\ArrayToModelTransformer;
 use Symfony\Component\Form\AbstractType;
 use Symfony\Component\Form\AbstractType;
 use Symfony\Component\Form\FormBuilderInterface;
 use Symfony\Component\Form\FormBuilderInterface;
@@ -53,10 +54,23 @@ class AdminType extends AbstractType
         if ($builder->getData() === null) {
         if ($builder->getData() === null) {
             $p = new PropertyAccessor(false, true);
             $p = new PropertyAccessor(false, true);
             try {
             try {
-                $subject = $p->getValue(
-                    $admin->getParentFieldDescription()->getAdmin()->getSubject(),
-                    $this->getFieldDescription($options)->getFieldName().$options['property_path']
-                );
+                // for PropertyAccessor < 2.5
+                // @todo remove this code for old PropertyAccessor after dropping support for Symfony 2.3
+                if (!method_exists($p, 'isReadable')) {
+                    $subjectCollection = $p->getValue(
+                        $admin->getParentFieldDescription()->getAdmin()->getSubject(),
+                        $this->getFieldDescription($options)->getFieldName()
+                    );
+                    if ($subjectCollection instanceof Collection) {
+                        $subject = $subjectCollection->get(trim($options['property_path'], '[]'));
+                    }
+                } else {
+                    // for PropertyAccessor >= 2.5
+                    $subject = $p->getValue(
+                        $admin->getParentFieldDescription()->getAdmin()->getSubject(),
+                        $this->getFieldDescription($options)->getFieldName().$options['property_path']
+                    );
+                }
                 $builder->setData($subject);
                 $builder->setData($subject);
             } catch (NoSuchIndexException $e) {
             } catch (NoSuchIndexException $e) {
                 // no object here
                 // no object here