/** * Returns already set options for the given option set and entity * * @param FieldConfigModel $configFieldModel * @param int $entityId * @return int[] */ protected function getSavedOptionIds(FieldConfigModel $configFieldModel, $entityId) { $savedOptionIds = $this->relations->findByFieldId($configFieldModel->getId(), $entityId); array_walk($savedOptionIds, function (&$item) { $item = $item->getOption()->getId(); }); return $savedOptionIds; }
public function preSubmitData(FormEvent $event) { list($entityId, $model) = $this->prepareEvent($event); $saved = []; if ($entityId) { $saved = $this->relations->findByFieldId($model->getId(), $entityId); array_walk($saved, function (&$item) { $item = $item->getOption()->getId(); }); } $data = $event->getData(); if (empty($data)) { $data = []; } if (!is_array($data)) { $data = [$data]; } if ($entityId) { /** * Save selected options */ $toSave = array_intersect($data, $saved); foreach ($data as $option) { if (!in_array($option, $saved)) { $optionRelation = new OptionSetRelation(); $optionRelation->setData(null, $entityId, $model, $this->options->find($option)); $toSave[] = $option; $this->em->persist($optionRelation); } } /** * Remove unselected */ if ($entityId && $this->relations->count($model->getId(), $entityId)) { $toRemove = $this->relations->findByNotIn($model->getId(), $entityId, $toSave); foreach ($toRemove as $option) { $this->em->remove($option); } } } }