* added base classes for repositories and queries #40 * prepared controller and repositories to use fetchByQuery() #40 * removed findAll() which has added pagination to the original doctrine method #40
This commit is contained in:
@@ -11,20 +11,17 @@
|
||||
|
||||
namespace TimesheetBundle\Repository;
|
||||
|
||||
use AppBundle\Entity\User;
|
||||
use AppBundle\Repository\AbstractRepository;
|
||||
use TimesheetBundle\Entity\Project;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Doctrine\ORM\Query;
|
||||
use Pagerfanta\Adapter\DoctrineORMAdapter;
|
||||
use Pagerfanta\Pagerfanta;
|
||||
use TimesheetBundle\Model\ProjectStatistic;
|
||||
use TimesheetBundle\Repository\Query\ProjectQuery;
|
||||
|
||||
/**
|
||||
* Class ProjectRepository
|
||||
*
|
||||
* @author Kevin Papst <kevin@kevinpapst.de>
|
||||
*/
|
||||
class ProjectRepository extends EntityRepository
|
||||
class ProjectRepository extends AbstractRepository
|
||||
{
|
||||
|
||||
/**
|
||||
@@ -54,66 +51,27 @@ class ProjectRepository extends EntityRepository
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @return Query
|
||||
* @param ProjectQuery $query
|
||||
* @return \Pagerfanta\Pagerfanta
|
||||
*/
|
||||
protected function queryLatest(User $user = null)
|
||||
public function findByQuery(ProjectQuery $query)
|
||||
{
|
||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||
|
||||
$qb->select('p')
|
||||
// if we join activities, the maxperpage limit will limit the list due to the raised amount of rows by projects * activities
|
||||
$qb->select('p', 'c')
|
||||
->from('TimesheetBundle:Project', 'p')
|
||||
->orderBy('p.id', 'DESC');
|
||||
->join('p.customer', 'c')
|
||||
->orderBy('p.' . $query->getOrderBy(), $query->getOrder());
|
||||
|
||||
return $qb->getQuery();
|
||||
}
|
||||
if ($query->getVisibility() === ProjectQuery::SHOW_VISIBLE) {
|
||||
$qb->andWhere('p.visible = 1');
|
||||
// TODO check for visibility of customer
|
||||
} elseif ($query->getVisibility() === ProjectQuery::SHOW_HIDDEN) {
|
||||
$qb->andWhere('p.visible = 0');
|
||||
// TODO check for visibility of customer
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $orderBy
|
||||
* @return Query
|
||||
*/
|
||||
protected function queryAll($orderBy = 'id')
|
||||
{
|
||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||
|
||||
$qb->select('p')
|
||||
->from('TimesheetBundle:Project', 'p')
|
||||
->orderBy('p.' . $orderBy, 'ASC');
|
||||
|
||||
return $qb->getQuery();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @param int $page
|
||||
* @return Pagerfanta
|
||||
*/
|
||||
public function findLatest(User $user, $page = 1)
|
||||
{
|
||||
return $this->getPager($this->queryLatest($user), $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $page
|
||||
*
|
||||
* @return Pagerfanta
|
||||
*/
|
||||
public function findAll($page = 1)
|
||||
{
|
||||
return $this->getPager($this->queryAll(), $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Query $query
|
||||
* @param int $page
|
||||
* @return Pagerfanta
|
||||
*/
|
||||
protected function getPager(Query $query, $page = 1)
|
||||
{
|
||||
$paginator = new Pagerfanta(new DoctrineORMAdapter($query, false));
|
||||
$paginator->setMaxPerPage(25);
|
||||
$paginator->setCurrentPage($page);
|
||||
|
||||
return $paginator;
|
||||
return $this->getPager($qb->getQuery(), $query->getPage(), $query->getPageSize());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user