Removed MergeCollectionListener because merging is done in the model
This commit is contained in:
parent
7fb0de3574
commit
a5ddda4de4
|
@ -1,50 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This file is part of the PropelBundle package.
|
|
||||||
* For the full copyright and license information, please view the LICENSE
|
|
||||||
* file that was distributed with this source code.
|
|
||||||
*
|
|
||||||
* @license MIT License
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Propel\PropelBundle\Form\EventListener;
|
|
||||||
|
|
||||||
use Symfony\Component\Form\FormEvents;
|
|
||||||
use Symfony\Component\Form\Event\FilterDataEvent;
|
|
||||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
||||||
|
|
||||||
class MergeCollectionListener implements EventSubscriberInterface
|
|
||||||
{
|
|
||||||
public static function getSubscribedEvents()
|
|
||||||
{
|
|
||||||
return array(FormEvents::BIND_NORM_DATA => 'onBindNormData');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function onBindNormData(FilterDataEvent $event)
|
|
||||||
{
|
|
||||||
$collection = $event->getForm()->getData();
|
|
||||||
$data = $event->getData();
|
|
||||||
|
|
||||||
if (!$collection) {
|
|
||||||
$collection = $data;
|
|
||||||
} else if (count($data) === 0) {
|
|
||||||
$collection->clear();
|
|
||||||
} else {
|
|
||||||
// merge $data into $collection
|
|
||||||
foreach ($collection as $i => $model) {
|
|
||||||
if (false === $key = $data->search($model)) {
|
|
||||||
$collection->remove($i);
|
|
||||||
} else {
|
|
||||||
$data->remove($key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($data as $model) {
|
|
||||||
$collection->append($model);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$event->setData($collection);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -23,8 +23,7 @@ class ModelType extends AbstractType
|
||||||
public function buildForm(FormBuilder $builder, array $options)
|
public function buildForm(FormBuilder $builder, array $options)
|
||||||
{
|
{
|
||||||
if ($options['multiple']) {
|
if ($options['multiple']) {
|
||||||
$builder->addEventSubscriber(new MergeCollectionListener())
|
$builder->prependClientTransformer(new ModelsToArrayTransformer($options['choice_list']));
|
||||||
->prependClientTransformer(new ModelsToArrayTransformer($options['choice_list']));
|
|
||||||
} else {
|
} else {
|
||||||
$builder->prependClientTransformer(new ModelToIdTransformer($options['choice_list']));
|
$builder->prependClientTransformer(new ModelToIdTransformer($options['choice_list']));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue