/** * Get all foreign entities ordered by a sort column * * If you have applied a limit to the query but need to know the full size of the unfiltered set, you must set * $check_full_set_size to true to gather this information at the expense of a second database query. * * @param SortedQuery $query * @param bool $check_full_set_size * @param bool $use_cache * @return QueryResult */ public function sortedQuery(SortedQuery $query, $check_full_set_size = false, $use_cache = true) { $metadata = $this->getMapper()->getEntityMetadata($query->getClassName()); if ($query->getRelationshipName()) { // Entity relationship based query $reader = new Reader($metadata, $query->getEntity()); $relationship = $metadata->getRelationshipByName($query->getRelationshipName()); if (!$relationship) { throw new InvalidArgumentException('Relationship "' . $query->getRelationshipName() . '" does not exist'); } // Important, else the QueryResult class will try to hydrate the wrong entity $query->setClassName($relationship->getTarget()); $key = $this->getKeyScheme()->getSortIndexKey($relationship, $query->getSortBy(), $reader->getId()); } else { // Table based query $key = $this->getKeyScheme()->getTableSortKey($metadata->getTableName(), $query->getSortBy()); } $results = $this->getDriver()->getSortedIndex($key, $query->getDirection() == Direction::DESC(), $query->getStart(), $query->getEnd()); if (!$query->getStart() && !$query->getEnd()) { $full_size = count($results); } elseif ($check_full_set_size) { $full_size = $this->getDriver()->getSortedIndexSize($key); } else { $full_size = null; } return new QueryResult($this->entity_manager, $query, $results, $full_size, $use_cache); }
/** * @param string $class_name Class name of table to query * @param string $sort_by Sort by column * @param Direction $direction Assumes ascending if omitted * @param int $start Start index (inclusive), null/0 for beginning of set * @param int $end Stop index (inclusive), null/-1 for end of set, -2 for penultimate record */ public function __construct($class_name, $sort_by, Direction $direction = null, $start = null, $end = null) { parent::__construct($class_name, null, $sort_by, $direction, $start, $end); }