/** * Get the relation entity * @return \Spot\Entity\EntityInterface */ public function entity() { if (null === $this->entity) { $this->entity = $this->execute(); if ($this->entity instanceof Query) { $this->entity = $this->entity->first(); } else { if ($this->entity instanceof ResultsetInterface) { $this->entity = $this->entity->first(); } } } return $this->entity; }
/** * {@inheritDoc} */ public function read(Query $query) { $builder = $query->builder(); $queryHash = md5($builder->getSql() . '_' . serialize($builder->getParameters())); if ($this->mapper instanceof MapperCacheInterface) { if ($builder->getType() == 0) { $collection = $this->mapper->getQueryFromCache($queryHash); if ($collection) { return $collection; } } else { $this->mapper->clearQueryCache(); } } /* if ($query->builder()->getType() == 0) { $clonedQuery = clone $query->builder(); $res = $clonedQuery->getConnection()->executeQuery('EXPLAIN ' . $clonedQuery->getSQL(), $clonedQuery->getParameters(), $clonedQuery->getParameterTypes()); $res->setFetchMode(\PDO::FETCH_ASSOC); echo '<pre>'; var_dump($queryHash); var_dump($clonedQuery->getSQL()); var_dump($clonedQuery->getParameters()); var_dump($res->fetchAll()); echo '</pre>'; } */ $stmt = $builder->execute(); $stmt->setFetchMode(\PDO::FETCH_ASSOC); $collection = $query->mapper()->collection($stmt, $query->with()); $stmt->closeCursor(); if ($this->mapper instanceof MapperCacheInterface && $builder->getType() == 0) { $this->mapper->addQueryToCache($queryHash, $collection); } return $collection; }
/** * Execute provided query and return result * * @param \Spot\Query $query SQL query to execute * @return \Doctrine\DBAL\Driver\Statement|int * @throws \Spot\Exception */ public function exec(Query $query) { return $query->builder()->execute(); }
/** * Return result set for current query */ public function toCollection(\Spot\Query $query, $stmt) { $mapper = $query->mapper(); $entityClass = $query->entityName(); if ($stmt instanceof \PDOStatement) { // Set PDO fetch mode $stmt->setFetchMode(\PDO::FETCH_ASSOC); $collection = $mapper->collection($entityClass, $stmt, $query->with()); // Ensure statement is closed $stmt->closeCursor(); return $collection; } else { $mapper->addError(__METHOD__ . " - Unable to execute query " . implode(' | ', $this->connection()->errorInfo())); return array(); } }
/** * Return result set for current query */ public function toCollection(\Spot\Query $query, \MongoCursor $cursor) { $mapper = $query->mapper(); $entityClass = $query->entityName(); if ($cursor instanceof MongoCursor) { // Set timeout if (isset($this->options['cursor']['timeout']) && is_int($this->options['cursor']['timeout'])) { $cursor->timeout($this->options['cursor']['timeout']); } return $mapper->collection($entityClass, $cursor); } else { $mapper->addError(__METHOD__ . " - Unable to execute query - not a valid MongoCursor"); return array(); } }
public function testQueryPagerExtension() { $mapper = test_spot_mapper(); \Spot\Query::addMethod('page', function (\Spot\Query $query, $limit, $perPage = 20) { return $query->limit($limit, $perPage); }); $posts = $mapper->all('\\Spot\\Entity\\Post')->page(1, 1); // Do this instead of $posts->count() because it drops LIMIT clause to count the full dataset $postCount = count($posts->toArray()); $this->assertEquals(1, $postCount); }