I have an archive table (id, title, author) and a download_history table (id, id_archive, date) to keep track of how many download for each archive. Now I want to build a report to view the most downloaded archives on a weekly bases. Pretty simple to do in SQL, but things are more complicated with doctrine, this is what I've done so far:
public function getTopDownloadsLastXDays($limit=100, $days=7)
{
$rsm = new ResultSetMapping();
$rsm->addEntityResult('DownloadHistory', 'd');
$rsm->addMetaResult('d', 'archive', 'archive');
$rsm->addScalarResult('tot', 'tot');
$rsm->addJoinedEntityResult('Archive', 'a', 'd', 'archive');
$rsm->addFieldResult('a', 'author', 'author');
$rsm->addFieldResult('a', 'title', 'title');
$sql = "SELECT
DISTINCT(dh.archive_id),
COUNT(*) as tot,
a.author,
a.title
FROM
download_history dh
INNER JOIN
archive a
ON
dh.archive_id = a.id
WHERE
dh.date >= DATE(NOW()) - INTERVAL :days DAY
GROUP BY
dh.archive_id
LIMIT :limit";
$em = $this->getEntityManager();
$query = $em->createNativeQuery($sql, $rsm);
$query->setParameters(array(
':days' => $days,
':limit' => $limit,
));
$results = $query->getResult();
return $results;
}
Entities exist and work fine. I followed the third example here: http://doctrine-orm.readthedocs.org/en/latest/reference/native-sql.html#examples. This is my result:
array(1) {
[-1]=> array(1) {
["tot"]=> string(1) "1"
}
}
any idea? thanks