find($id); } /** * @param $entity * @param bool $sync * @return mixed|void * @throws \Doctrine\ORM\ORMException * @throws \Doctrine\ORM\OptimisticLockException */ public function add($entity, $sync = false) { $this->_em->persist($entity); if($sync) $this->_em->flush($entity); } /** * @param IEntity $entity * @return void */ public function delete($entity) { $this->_em->remove($entity); } /** * @return IEntity[] */ public function getAll() { return $this->findAll(); } /** * @return string */ protected abstract function getBaseEntity(); /** * @return array */ protected abstract function getFilterMappings(); /** * @return array */ protected abstract function getOrderMappings(); /** * @param QueryBuilder $query * @return QueryBuilder */ protected abstract function applyExtraFilters(QueryBuilder $query); /** * @param PagingInfo $paging_info * @param Filter|null $filter * @param Order|null $order * @return PagingResponse */ public function getAllByPage(PagingInfo $paging_info, Filter $filter = null, Order $order = null){ $query = $this->getEntityManager() ->createQueryBuilder() ->select("e") ->from($this->getBaseEntity(), "e"); $query = $this->applyExtraFilters($query); if(!is_null($filter)){ $filter->apply2Query($query, $this->getFilterMappings()); } if(!is_null($order)){ $order->apply2Query($query, $this->getOrderMappings()); } $query= $query ->setFirstResult($paging_info->getOffset()) ->setMaxResults($paging_info->getPerPage()); $paginator = new Paginator($query, $fetchJoinCollection = true); $total = $paginator->count(); $data = array(); foreach($paginator as $entity) array_push($data, $entity); return new PagingResponse ( $total, $paging_info->getPerPage(), $paging_info->getCurrentPage(), $paging_info->getLastPage($total), $data ); } }