Ejemplo n.º 1
0
    static function factory(EntityManager $em, $uuid, $allowCache = false)
    {
        if (!Util\UUID::validate($uuid)) {
            throw new \Exception('Invalid UUID: \'' . $uuid . '\'');
        }
        if (!self::$cache) {
            self::$cache = $em->getConfiguration()->getQueryCacheImpl();
        }
        if ($allowCache && self::$cache && self::$cache->contains($uuid)) {
            // used hydrated cache result
            return self::$cache->fetch($uuid);
        }
        $dql = 'SELECT a, p
			FROM Volkszaehler\\Model\\Entity a
			LEFT JOIN a.properties p
			WHERE a.uuid = :uuid';
        $q = $em->createQuery($dql)->setParameter('uuid', $uuid);
        try {
            $entity = $q->getSingleResult();
            if ($allowCache && self::$cache) {
                self::$cache->save($uuid, $entity, Util\Configuration::read('cache.ttl'));
            }
            return $entity;
        } catch (\Doctrine\ORM\NoResultException $e) {
            throw new \Exception('No entity found with UUID: \'' . $uuid . '\'');
        }
    }
Ejemplo n.º 2
0
    /**
     * Get entity
     *
     * @param string $identifier
     */
    public function get($uuid = NULL)
    {
        if (isset($uuid)) {
            // single entity
            if (!Util\UUID::validate($uuid)) {
                throw new \Exception('Invalid UUID: \'' . $uuid . '\'');
            }
            $dql = 'SELECT a, p
				FROM Volkszaehler\\Model\\Entity a
				LEFT JOIN a.properties p
				WHERE a.uuid = :uuid';
            $q = $this->em->createQuery($dql);
            $q->setParameter('uuid', $uuid);
            try {
                return $q->getSingleResult();
            } catch (\Doctrine\ORM\NoResultException $e) {
                throw new \Exception('No entity found with UUID: \'' . $uuid . '\'', 404);
            }
        } else {
            // public entities
            return array('entities' => $this->filter(array('public' => TRUE)));
        }
    }