/** * {@inheritdoc} */ public function save(ClassMetadata $meta, Component $component, $entity) { if (!$component instanceof ToManyContainer) { return FALSE; } if (!($collection = $this->getCollection($meta, $entity, $component->getName()))) { return FALSE; } $em = $this->mapper->getEntityManager(); $class = $meta->getAssociationTargetClass($component->getName()); $relationMeta = $em->getClassMetadata($class); /** @var Nette\Forms\Container $container */ foreach ($component->getComponents(FALSE, 'Nette\\Forms\\Container') as $container) { $isNew = substr($container->getName(), 0, strlen(ToManyContainer::NEW_PREFIX)) === ToManyContainer::NEW_PREFIX; $name = $isNew ? substr($container->getName(), strlen(ToManyContainer::NEW_PREFIX)) : $container->getName(); if (!($relation = $collection->get($name))) { // entity was added from the client if (!$component->isAllowedRemove()) { continue; } $collection[$name] = $relation = $relationMeta->newInstance(); } $this->mapper->save($relation, $container); } return TRUE; }
/** * @param ClassMetadata $meta * @param object $entity * @param string $field * @return bool|object */ private function getRelation(ClassMetadata $meta, $entity, $field) { if (!$meta->hasAssociation($field) || !$meta->isSingleValuedAssociation($field)) { return FALSE; } // todo: allow access using property or method $relation = $meta->getFieldValue($entity, $field); if ($relation instanceof Collection) { return FALSE; } if ($relation === NULL) { $class = $meta->getAssociationTargetClass($field); $relationMeta = $this->mapper->getEntityManager()->getClassMetadata($class); $relation = $relationMeta->newInstance(); $meta->setFieldValue($entity, $field, $relation); } return $relation; }
protected function save(Form $form, $entity) { try { $this->entityFormMapper->save($entity, $form); $this->entityManager->persist($entity); $this->entityManager->flush($entity); } catch (\Exception $e) { $this->error($form, $e); } }
/** * @return \Venne\Forms\IFormFactory */ public function create() { $form = parent::create(); $form['_eventControl'] = $eventControl = new EventControl('_eventControl'); $eventControl->onAttached[] = function () use($form) { $this->entityMapper->load($this->entity, $form); unset($form['_eventControl']); }; $form->onSuccess[] = function (Form $form) { $this->entityMapper->save($this->entity, $form); $saveEntity = $this->saveEntity; if ($saveEntity && $saveEntity($form)) { try { $this->entityMapper->getEntityManager()->flush($this->entity); } catch (\Exception $e) { Debugger::log($e); $form->addError($e->getMessage()); } } }; return $form; }
public function buildField($name) { if (count($relation = explode('.', $name, 2)) == 2) { return $this->relationBuilder($relation[0])->buildField($relation[1]); } try { $mapping = $this->getMetadata()->getFieldMapping($name); } catch (MappingException $e) { throw new InvalidArgumentException($e->getMessage(), 0, $e); } $control = $this->controlFactory->create($this->getMetadata(), $mapping); $this->container->addComponent($control, $name); $this->mapper->load($this->entity, $control); return $control; }
public function __construct(EntityFormMapper $mapper) { $this->mapper = $mapper; $this->em = $this->mapper->getEntityManager(); $this->accessor = $mapper->getAccessor(); }