Пример #1
0
    /**
     * 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 (!$this->loaded) {
            $this->load();
        }

        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->findOne($this->class, $key);
    }
Пример #2
0
 /**
  * Returns the target object
  *
  * @param integer $id
  * @return object
  */
 public function getObject($id)
 {
     return $this->modelManager->findOne($this->getClass(), $id);
 }