Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList::getEntity PHP Method

getEntity() public method

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.
public getEntity ( string $key ) : object
$key string 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 (!$this->loaded) {
            $this->load();
        }

        try {
            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;
            } 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->em->find($this->class, $key);
        } catch (NoResultException $e) {
            return null;
        }
    }