replaced repository methods with query-builder ones

This commit is contained in:
Andrey
2013-08-13 19:57:21 +02:00
parent 3b3d08a9e1
commit 090f230dd9
3 changed files with 38 additions and 25 deletions

View File

@@ -92,7 +92,7 @@ class Aggregate implements FeedInterface
*/ */
public static function create(Application $app, array $feed_ids) public static function create(Application $app, array $feed_ids)
{ {
$feeds = $this->em->getRepository('Entities\Feed')->findByIdArray($feed_ids); $feeds = $this->em->getRepository('Entities\Feed')->findByIds($feed_ids);
return new static($app, $feeds); return new static($app, $feeds);
} }
@@ -114,7 +114,11 @@ class Aggregate implements FeedInterface
return null; return null;
} }
return $this->em->getRepository('Entities\FeedEntry')->findByFeeds($this->feeds, $offset_start, $how_many); $feedIds = array();
foreach ($this->feeds as $feed) {
$feedIds[] = $feed->getId();
}
return $this->em->getRepository('Entities\FeedEntry')->findByFeeds($feedIds, $offset_start, $how_many);
} }
/** /**
@@ -195,7 +199,11 @@ class Aggregate implements FeedInterface
public function getCountTotalEntries() public function getCountTotalEntries()
{ {
if (count($this->feeds) > 0) { if (count($this->feeds) > 0) {
return count($this->em->getRepository('Entities\FeedEntry')->findByFeeds($this->feeds)); $feedIds = array();
foreach ($this->feeds as $feed) {
$feedIds[] = $feed->getId();
}
return count($this->em->getRepository('Entities\FeedEntry')->findByFeeds($feedIds));
} }
return 0; return 0;

View File

@@ -78,14 +78,16 @@ class SessionRepository extends EntityRepository
* @param array $feed_id * @param array $feed_id
* @return Collection * @return Collection
*/ */
public function findByIdArray(array $feed_id) public function findByIds(array $feedIds)
{ {
$dql = 'SELECT f FROM Entities\Feed f $qb = $this->createQueryBuilder('f');
ORDER BY f.created_on DESC
WHERE f.id IN (' . implode(",", $feed_id) . ')';
$query = $this->_em->createQuery($dql); if (!empty($feedIds)) {
$qb->Where($qb->expr()->in('f.id', $feedIds));
return $query->getResult();
} }
$qb->orderBy('f.updated_on', 'DESC');
return $qb->getQuery()->getResult();
}
} }

View File

@@ -25,26 +25,29 @@ class UserSettingRepository extends EntityRepository
* Returns a collection of FeedEntry from given feeds, limited to $how_many results, starting with $offset_start * Returns a collection of FeedEntry from given feeds, limited to $how_many results, starting with $offset_start
* *
* @param array $feeds * @param array $feeds
* @param integer $offset_start * @param integer $offsetStart
* @param integer $how_many * @param integer $howMany
* *
* @return \Doctrine\Common\Collections\Collection * @return \Doctrine\Common\Collections\Collection
*/ */
public function findByFeeds($feeds, $offset_start = null, $how_many = null) public function findByFeeds($feeds, $offsetStart = null, $perPage = null)
{ {
$dql = 'SELECT f FROM Entities\FeedEntry f $qb = $this->createQueryBuilder('f');
WHERE f.feed IN (:feeds) order by f.updated_on DESC';
$query = $this->_em->createQuery($dql); if (!empty($feeds)) {
$query->setParameter('feeds', $feeds); $qb->Where($qb->expr()->in('f.feed', $feeds));
if (null !== $offset_start && 0 !== $offset_start) {
$query->setFirstResult($offset_start);
}
if (null !== $how_many) {
$query->setMaxResults($how_many);
} }
return $query->getResult(); $qb->orderBy('f.updated_on', 'DESC');
if ($offsetStart) {
$qb->setFirstResult(max(0, (int) $offsetStart));
}
if ($perPage) {
$qb->setMaxResults(max(5, (int) $perPage));
}
return $qb->getQuery()->getResult();
} }
} }