我有一个存档表(ID,标题,作者)和一个download_history表(ID,ID_ARCHIVE,DATE),以跟踪每个存档的下载多少。现在,我想建立一个报告,以每周的基础查看下载最多的档案。在SQL中做得很简单,但是学说的情况更为复杂,这是我到目前为止所做的:
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;
}
实体存在并正常工作。我在这里遵循第三个示例:http://doctrine-orm.readthedocs.org/en/latest/reference/native-sql.html#examples。这是我的结果:
array(1) {
[-1]=> array(1) {
["tot"]=> string(1) "1"
}
}
有什么想法吗?谢谢
您将表格为" dh",但是在构建结果设置映射时将别名指定为" D"。