【发布时间】:2015-07-27 14:53:23
【问题描述】:
我在使用 Doctrine2 时遇到了一件非常简单的事情,我无法弄清楚如何使用我的 findByDate 方法从存储库中检索实体的最后一个条目。 我无法在 Doctrine 文档或谷歌中找到如何做到这一点......
【问题讨论】:
-
你也可以试试max(id)函数
标签: symfony doctrine-orm doctrine
我在使用 Doctrine2 时遇到了一件非常简单的事情,我无法弄清楚如何使用我的 findByDate 方法从存储库中检索实体的最后一个条目。 我无法在 Doctrine 文档或谷歌中找到如何做到这一点......
【问题讨论】:
标签: symfony doctrine-orm doctrine
您必须执行按日期排序的查询,并返回第一个:
class MyEntityRepository extends EntityRepository
{
function getLastEntity() {
return $this->createQueryBuilder('e')->
orderBy('e.date', 'DESC')->
setMaxResults(1)->
getQuery()->
getOneOrNullResult();
}
}
【讨论】:
试试:
$date = new \Datetime();
$em = $this->getDoctrine()->getManager();
$lastEntity = $em
->getRepository('MyBundle:Entity')
->findBy(array('date' => $date->format('Y-m-d')));
假设 MySQL 中的字段存储为date。
【讨论】: