/** * Constructor * * @param string $entityClass Name of Doctrine entity to be paginated * @param string $sortField Name of Doctrine entity's property to sort results by * @param string [Optional] $sortOrder Sort order (i.e. 'ASC' or 'DESC'). Defaults to 'ASC'. * @param string [Optional] $where Where clause * @return void */ public function __construct($entityClass, $sortField = null, $sortOrder = 'ASC', $where = null) { $this->entityClass = $entityClass; $this->sortField = $sortField; $this->sortOrder = $sortOrder; $this->where = $where; $queryBuilder = DoctrineService::getEntityManager()->createQueryBuilder(); $queryBuilder->select('e')->from($this->entityClass, 'e'); if (null !== $this->sortField) { $part = explode('.', $this->sortField); if (isset($part[1])) { $foreignColName = $part[1]; $foreignEntityName = lcfirst(array_pop(explode('\\', $part[0]))); $queryBuilder->leftJoin('e.' . $foreignEntityName, 'j')->orderBy('j.' . $foreignColName, $this->sortOrder); } else { $queryBuilder->orderBy('e.' . $this->sortField, $this->sortOrder); } } if (null !== $this->where && '' !== $this->where) { $queryBuilder->where($this->where); } $this->query = $queryBuilder; $this->countQuery = clone $this->query; }