Ok I have 2 tables, the corresponding structure is as follows:
**youtubevideo**
id - int
allViews - relational
**views**
id - int
youtubevideo_id - int
viewCount - int
firstFetch - date
Accordingly, youtubevideo has many views. This is represented by OneToMany in a Symfony object.
Now I have 2 dates, let's call them fromDate and toDate. I need to select all youtubevideos and order them by the sum of the viewCount of the views that belong to them.
And I just want to use views that have their firstFetch date, between fromDate and toDate.
So, I think it will be something like
SELECT y FROM YTScraperBundle:YouTubeVideo y
JOIN y.allViews v GROUP BY y.id
ORDER BY SUM(v.viewCount) LIMIT 0, 30; <-- or does this need to be an inline select to the specific element?
And I really don't see how to set WHERE v.firstFetch between them and fromDate.
UPDATE
$query = $this->em->createQuery(
"SELECT y, IF(v IS NULL, 0, SUM(v.viewCount)) AS HIDDEN sumviewcount
FROM YTScraperBundle:YouTubeVideo y
LEFT JOIN y.allViews v WITH v.firstFetch BETWEEN :fromDate AND :toDate
GROUP BY y ORDER BY sumviewcount DESC
"
);
$query->setMaxResults(self::PR_PAGE);
$query->setFirstResult($firstResult);
$query->setParameter('fromDate', $fromDate);
$query->setParameter('toDate', $toDate);
Getting error:
Expected known function, got 'IF'
source
share