/** * {@inheritdoc} */ public function load(ClassMetadata $meta, Component $component, $entity) { if (!$component instanceof BaseControl) { return FALSE; } if ($meta->hasField($name = $component->getOption(self::FIELD_NAME, $component->getName()))) { $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) && !count($component->getItems()) && !$component->getOption(self::FIELD_NOT_LOAD, false)) { 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 ($relation = $this->accessor->getValue($entity, $name)) { $UoW = $this->em->getUnitOfWork(); $component->setValue($UoW->getSingleIdentifierValue($relation)); } return TRUE; }
/** * @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; }