Browse Source

Fixed bug in sonata_type_collection that calls entity's remove method with null argument.

This bug occurs in Symfony 2.3 with old version of PropertyAccessor.
Andrej Hudec 10 năm trước cách đây
mục cha
commit
0e7b11c5e2
1 tập tin đã thay đổi với 18 bổ sung4 xóa
  1. 18 4
      Form/Type/AdminType.php

+ 18 - 4
Form/Type/AdminType.php

@@ -11,6 +11,7 @@
 
 namespace Sonata\AdminBundle\Form\Type;
 
+use Doctrine\Common\Collections\Collection;
 use Sonata\AdminBundle\Form\DataTransformer\ArrayToModelTransformer;
 use Symfony\Component\Form\AbstractType;
 use Symfony\Component\Form\FormBuilderInterface;
@@ -53,10 +54,23 @@ class AdminType extends AbstractType
         if ($builder->getData() === null) {
             $p = new PropertyAccessor(false, true);
             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);
             } catch (NoSuchIndexException $e) {
                 // no object here