/** * {@inheritdoc} */ public function reverseTransform($newId) { if (empty($newId) && !in_array($newId, array('0', 0), true)) { return; } return $this->modelManager->find($this->className, $newId); }
/** * Returns the entity for the given key. * * If the underlying entities have composite identifiers, the choices * are initialized. The key is expected to be the index in the choices * array in this case. * * If they have single identifiers, they are either fetched from the * internal entity cache (if filled) or loaded from the database. * * @param string $key The choice key (for entities with composite * identifiers) or entity ID (for entities with single * identifiers) * * @return object The matching entity */ public function getEntity($key) { if (count($this->identifier) > 1) { // $key is a collection index $entities = $this->getEntities(); return isset($entities[$key]) ? $entities[$key] : null; } elseif ($this->entities) { return isset($this->entities[$key]) ? $this->entities[$key] : null; } return $this->modelManager->find($this->class, $key); }
/** * {@inheritdoc} */ public function reverseTransform($keys) { if (!is_array($keys)) { throw new UnexpectedTypeException($keys, 'array'); } $collection = $this->modelManager->getModelCollectionInstance($this->class); $notFound = array(); // optimize this into a SELECT WHERE IN query foreach ($keys as $key) { if ($entity = $this->modelManager->find($this->class, $key)) { $collection[] = $entity; } else { $notFound[] = $key; } } if (count($notFound) > 0) { throw new TransformationFailedException(sprintf('The entities with keys "%s" could not be found', implode('", "', $notFound))); } return $collection; }
/** * {@inheritdoc} */ public function reverseTransform($value) { $collection = $this->modelManager->getModelCollectionInstance($this->className); if (empty($value)) { if ($this->multiple) { return $collection; } return; } if (!$this->multiple) { return $this->modelManager->find($this->className, $value); } if (!is_array($value)) { throw new \UnexpectedValueException(sprintf('Value should be array, %s given.', gettype($value))); } foreach ($value as $key => $id) { if ($key === '_labels') { continue; } $collection->add($this->modelManager->find($this->className, $id)); } return $collection; }
/** * Returns the entity for the given key * * If the underlying entities have composite identifiers, the choices * are intialized. The key is expected to be the index in the choices * array in this case. * * If they have single identifiers, they are either fetched from the * internal entity cache (if filled) or loaded from the database. * * @param string $key The choice key (for entities with composite * identifiers) or entity ID (for entities with single * identifiers) * @return object The matching entity */ public function getEntity($key) { if (count($this->identifier) > 1) { // $key is a collection index $entities = $this->getEntities(); return isset($entities[$key]) ? $entities[$key] : null; } else { if ($this->entities) { return isset($this->entities[$key]) ? $this->entities[$key] : null; } } // todo : I don't see the point of this .. // else if ($qb = $this->queryBuilder) { // // should we clone the builder? // $alias = $qb->getRootAlias(); // $where = $qb->expr()->eq($alias.'.'.current($this->identifier), $key); // // return $qb->andWhere($where)->getQuery()->getSingleResult(); // } return $this->modelManager->find($this->class, $key); }
/** * Initializes the choices and returns them * * The choices are generated from the entities. If the entities have a * composite identifier, the choices are indexed using ascending integers. * Otherwise the identifiers are used as indices. * * If the entities were passed in the "choices" option, this method * does not have any significant overhead. Otherwise, if a query builder * was passed in the "query_builder" option, this builder is now used * to construct a query which is executed. In the last case, all entities * for the underlying class are fetched from the repository. * * If the option "property" was passed, the property path in that option * is used as option values. Otherwise this method tries to convert * objects to strings using __toString(). * * @return array An array of choices */ protected function load() { parent::load(); if (is_array($this->choices)) { $entities = $this->choices; } else if ($this->query) { $entities = $this->modelManager->executeQuery($this->query); } else { $entities = $this->modelManager->find($this->class); } $this->choices = array(); $this->entities = array(); foreach ($entities as $key => $entity) { if ($this->propertyPath) { // If the property option was given, use it $value = $this->propertyPath->getValue($entity); } else { // Otherwise expect a __toString() method in the entity $value = (string)$entity; } if (count($this->identifier) > 1) { // When the identifier consists of multiple field, use // naturally ordered keys to refer to the choices $this->choices[$key] = $value; $this->entities[$key] = $entity; } else { // When the identifier is a single field, index choices by // entity ID for performance reasons $id = current($this->getIdentifierValues($entity)); $this->choices[$id] = $value; $this->entities[$id] = $entity; } } }