/** * @param \Symfony\Component\Form\Event\FilterDataEvent $event */ public function onBind(FilterDataEvent $event) { $collection = $event->getForm()->getData(); $data = $event->getData(); // looks like there is no way to remove other listeners $event->stopPropagation(); if (!$collection) { $collection = $data; } else { if (count($data) === 0) { $this->modelManager->collectionClear($collection); } else { // merge $data into $collection foreach ($collection as $entity) { if (!$this->modelManager->collectionHasElement($data, $entity)) { $this->modelManager->collectionRemoveElement($collection, $entity); } else { $this->modelManager->collectionRemoveElement($data, $entity); } } foreach ($data as $entity) { $this->modelManager->collectionAddElement($collection, $entity); } } } $event->setData($collection); }