【发布时间】:2012-08-25 06:00:42
【问题描述】:
我正在尝试进行分页,但出现错误:
[语法错误] line 0, col 57: Error: Expected end of string, got 'limit'
我不太确定这是否是进行查询的正确语法(和逻辑):
public function getFriendsFromTo ($user, $limit, $offset)
{
return $this->getEntityManager()
->createQuery('SELECT f FROM EMMyFriendsBundle:Friend f WHERE f.user='.$user.' limit '.$limit. 'offset' .$offset)
->getResult();
}
好友和用户关联manyToOne和oneToMany,所以在friends表中有一个字段——user_id。
这是在我的控制器中:
$user = $this->get('security.context')->getToken()->getUser();
$id = $user->getId();
$friends = $user->getFriends();
$result = count($friends)
$FR_PER_PAGE = 7;
$pages = $result/$FR_PER_PAGE;
$em = $this->getDoctrine()->getEntityManager();
$friends = $em->getRepository('EMMyFriendsBundle:Friend')
->getFriendsFromTo($id, $FR_PER_PAGE, $page*$FR_PER_PAGE);
我知道这很愚蠢,甚至是错误的(尤其是第三个参数是$page*$FR_PER_PAGE),但我只是想试试查询是否有效,但它没有。
【问题讨论】:
-
什么意思?得到任何错误?您还应该使用
setParameter("user", $user),而不是将其直接插入 DQL。此外,很高兴看到您的实体定义。编辑:刚刚看到编辑。 'offset' 后面应该有一个空格 -
是的,我收到一个错误 - 它在问题的开头。
-
您可能对这个捆绑包感兴趣:knpbundles.com/KnpLabs/KnpPaginatorBundle
-
谢谢 :) 但我更喜欢不打包,以便更好地理解它。
标签: symfony pagination doctrine-orm