A 有这样的代码:
$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery("SELECT p FROM AcmeBlogBundle:Publish p
ORDER BY p.update_date DESC");
$query->setFirstResult(($page-1)*$ads_on_page);
$query->setMaxResults($ads_on_page);
$posts = $query->getResult();
现在我正在尝试获取查询中的总行数(不取决于 setMeaResult 课程)。我尝试使用 count() 函数,但它不起作用......
所以我做了SELECT p, count(p.id) as p_count FROM AcmeBlogBundle:Publish p ORDER BY update_date DESC
并且它有效,但我看到现在我只有一行 [我认为它是因为count()
只返回一行]。所以我做了两个查询,如下所示:
$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery("SELECT p FROM AcmeBlogBundle:Publish p
ORDER BY p.update_date DESC");
$query->setFirstResult(($page-1)*$posts_on_page);
$query->setMaxResults($posts_on_page);
$count_query = $em->createQuery("SELECT COUNT(p.id) AS p_count FROM AcmeBlogBundle:Publish p
ORDER BY p.update_date DESC");
$posts = $query->getResult();
$count = $count_query->getResult();
它运作良好,但我想知道这是正确的方法吗?