在Doctrine2查询中使用限制和偏移

时间:2022-09-15 19:43:54

I'm trying to do the pagination, but there is an error:

我正在尝试分页,但是有一个错误:

[Syntax Error] line 0, col 57: Error: Expected end of string, got 'limit'

[语法错误]第0行,第57行:错误:字符串的预期结束,得到'限制'

I'm not quite sure if this is the right syntax (and logic) to make my query:

我不太确定这是否是正确的语法(和逻辑)来进行查询:

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();
}

Friends and users are related manyToOne and oneToMany, so in the friends table there is a field - user_id.

朋友和用户有很多关系,一个是OneOo和oneToMany,所以在好友表中有一个字段 - user_id。

This is in my controller:

这是在我的控制器中:

$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); 

I know that it's stupid and even wrong (especially the third parameter to be $page*$FR_PER_PAGE), but I just wanted to try if the query works, and it didn't.

我知道它是愚蠢甚至错误的(特别是第三个参数是$ page * $ FR_PER_PAGE),但我只是想尝试查询是否有效,但事实并非如此。

4 个解决方案

#1


106  

Nope. Use:

不。使用:

  return $this->getEntityManager()
        ->createQuery('...')
        ->setMaxResults(5)
        ->setFirstResult(10)
        ->getResult();

#2


27  

$towary = $this->getDoctrine()
   ->getRepository('AcmeStoreBundle:Towar') 
   ->findBy(array(),array(),10,($current-1)*$numItemsPerPage);

#3


14  

You can use findBy 3rd and 4th parameters of method of doctrine repository, which are limit and offset.

您可以使用docBine存储库方法的findBy第3和第4个参数,它们是限制和偏移量。

Here is the method definition:

这是方法定义:

findBy( array $criteria, array $orderBy = null, integer|null $limit = null, integer|null $offset = null )

findBy(array $ criteria,array $ orderBy = null,integer | null $ limit = null,integer | null $ offset = null)

Source: http://www.doctrine-project.org/api/orm/2.2/class-Doctrine.ORM.EntityRepository.html

资料来源:http://www.doctrine-project.org/api/orm/2.2/class-Doctrine.ORM.EntityRepository.html

#4


1  

you can also use

你也可以用

$query->getSingleResult();

$查询 - > getSingleResult();

#1


106  

Nope. Use:

不。使用:

  return $this->getEntityManager()
        ->createQuery('...')
        ->setMaxResults(5)
        ->setFirstResult(10)
        ->getResult();

#2


27  

$towary = $this->getDoctrine()
   ->getRepository('AcmeStoreBundle:Towar') 
   ->findBy(array(),array(),10,($current-1)*$numItemsPerPage);

#3


14  

You can use findBy 3rd and 4th parameters of method of doctrine repository, which are limit and offset.

您可以使用docBine存储库方法的findBy第3和第4个参数,它们是限制和偏移量。

Here is the method definition:

这是方法定义:

findBy( array $criteria, array $orderBy = null, integer|null $limit = null, integer|null $offset = null )

findBy(array $ criteria,array $ orderBy = null,integer | null $ limit = null,integer | null $ offset = null)

Source: http://www.doctrine-project.org/api/orm/2.2/class-Doctrine.ORM.EntityRepository.html

资料来源:http://www.doctrine-project.org/api/orm/2.2/class-Doctrine.ORM.EntityRepository.html

#4


1  

you can also use

你也可以用

$query->getSingleResult();

$查询 - > getSingleResult();