Skip to content

Commit

Permalink
Merge pull request #3128 from pulzarraider/collection_fix_null
Browse files Browse the repository at this point in the history
Fixed bug in sonata_type_collection that calls entity's remove method with null argument in Symfony 2.3
  • Loading branch information
pulzarraider committed Jul 30, 2015
2 parents de100fb + 0e7b11c commit 355eefa
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions Form/Type/AdminType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,10 +54,23 @@ public function buildForm(FormBuilderInterface $builder, array $options)
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
Expand Down

0 comments on commit 355eefa

Please sign in to comment.