/** * Return result set for current query */ public function toCollection(Spot_Query $query, MongoCursor $cursor) { $mapper = $query->mapper(); $entityClass = $query->entityName(); if ($cursor instanceof MongoCursor) { $results = array(); $resultsIdentities = array(); // Set timeout if (isset($this->options['cursor']['timeout']) && is_int($this->options['cursor']['timeout'])) { $cursor->timeout($this->options['cursor']['timeout']); } // Fetch all results into new entity class // @todo Move this to collection class so entities will be lazy-loaded by Collection iteration foreach ($cursor as $document) { $entity = new $entityClass(); $mapper->data($entity, $document); // Store in array for Collection $results[] = $entity; // Store primary key of each unique record in set $pk = $mapper->primaryKey($entity); if (!in_array($pk, $resultsIdentities) && !empty($pk)) { $resultsIdentities[] = $pk; } } $collectionClass = $mapper->collectionClass(); return new $collectionClass($results, $resultsIdentities); } else { $mapper->addError(__METHOD__ . " - Unable to execute query - not a valid MongoCursor"); return array(); } }
/** * Return result set for current query */ public function toCollection(Spot_Query $query, $stmt) { $mapper = $query->mapper(); $entityClass = $query->entityName(); if ($stmt instanceof PDOStatement) { $results = array(); $resultsIdentities = array(); // Set object to fetch results into $stmt->setFetchMode(PDO::FETCH_CLASS, $entityClass); // Fetch all results into new collection class while ($entity = $stmt->fetch(PDO::FETCH_CLASS)) { // Store in array for ResultSet $results[] = $entity; // Store primary key of each unique record in set $pk = $mapper->primaryKey($entity); if (!in_array($pk, $resultsIdentities) && !empty($pk)) { $resultsIdentities[] = $pk; } // Load relations $mapper->loadRelations($entity); } // Ensure set is closed $stmt->closeCursor(); $collectionClass = $mapper->collectionClass(); return new $collectionClass($results, $resultsIdentities); } else { $mapper->addError(__METHOD__ . " - Unable to execute query " . implode(' | ', $this->adapterRead()->errorInfo())); return array(); } }