/** * @param \Doctrine\ORM\Mapping\ClassMetadata $meta * @param \Nette\ComponentModel\Component $component * @param mixed $entity * @return boolean */ public function load(ClassMetadata $meta, Component $component, $entity) { if (!$component instanceof BaseControl) { return false; } $name = $component->getOption(self::FIELD_NAME, $component->getName()); if ($meta->hasField($name)) { $component->setValue($this->accessor->getValue($entity, $name)); return true; } if (!$meta->hasAssociation($name)) { return false; } /** @var SelectBox|RadioList $component */ if (($component instanceof SelectBox || $component instanceof RadioList || $component instanceof \Nette\Forms\Controls\MultiChoiceControl) && !count($component->getItems())) { if (!($nameKey = $component->getOption(self::ITEMS_TITLE, false))) { $path = $component->lookupPath('Nette\\Application\\UI\\Form'); throw new \Kdyby\DoctrineForms\InvalidStateException('Either specify items for ' . $path . ' yourself, or set the option Kdyby\\DoctrineForms\\IComponentMapper::ITEMS_TITLE ' . 'to choose field that will be used as title'); } $criteria = $component->getOption(self::ITEMS_FILTER, array()); $orderBy = $component->getOption(self::ITEMS_ORDER, array()); $related = $this->relatedMetadata($entity, $name); $items = $this->findPairs($related, $criteria, $orderBy, $nameKey); $component->setItems($items); } if ($meta->isCollectionValuedAssociation($name)) { $collection = $meta->getFieldValue($entity, $name); if ($collection instanceof PersistentCollection) { $values = array(); foreach ($collection as $value) { $values[] = $value->getId(); } $component->setDefaultValue($values); } } elseif ($relation = $this->accessor->getValue($entity, $name)) { $UoW = $this->entityManager->getUnitOfWork(); $component->setValue($UoW->getSingleIdentifierValue($relation)); } return true; }
/** * @param $value * @param Component $control */ protected function mapValueToForm($value, Component &$control) { $defaultValue = $value; if (is_object($value)) { if ($value instanceof \Tiplap\Doctrine\Entities\IdentifiedEntity) { $control->setValue($value->getId()); return; } else { if (get_class($value) === 'Doctrine\\ORM\\PersistentCollection') { $defaultValue = array(); /** @var IdentifiedEntity $entity */ foreach ($value->getValues() as $entity) { $defaultValue[] = $entity->getId(); } } } } if ($defaultValue !== NULL) { $control->setDefaultValue($defaultValue); } }