65

ページネーションを実行しようとしていますが、エラーがあります。

[構文エラー]行0、列57:エラー:文字列の終わりが必要です。「制限」を取得しました

これが私のクエリを作成するための正しい構文(およびロジック)であるかどうかはよくわかりません。

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

私はそれが愚かで間違っていることを知っていますが(特に3番目のパラメーター$page*$FR_PER_PAGE)、クエリが機能するかどうかを試してみたかったのですが、機能しませんでした。

4

5 に答える 5

154

いいえ。使用する:

  return $this->getEntityManager()
        ->createQuery('...')
        ->setMaxResults(5)
        ->setFirstResult(10)
        ->getResult();
于 2012-08-30T13:38:29.180 に答える
35
$towary = $this->getDoctrine()
   ->getRepository('AcmeStoreBundle:Towar') 
   ->findBy(array(),array(),10,($current-1)*$numItemsPerPage);
于 2013-11-02T19:19:01.610 に答える
22

findBy教義リポジトリのメソッドの3番目と4番目のパラメータであるとlimitを使用できますoffset

メソッドの定義は次のとおりです。

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

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

于 2015-12-22T17:12:26.350 に答える
1

あなたも使うことができます

$ query-> getSingleResult();

于 2014-11-12T10:14:32.123 に答える
0

Doctrine2.6は、この古い投稿に出くわし、DQLの方法を試しましたが、目的に適合しませんでした。したがって、エンティティが既にマップされて結合されているためにDQLの使用を避けたい場合は、マッチングと基準を使用してページングを実行できます。

$criteria = Criteria::create()
            ->setMaxResults($limit ? $limit : null)
            ->setFirstResult($offset ? $offset : null)
$result = $em->getRepository('EMMyFriendsBundle:Friend')
            ->matching($criteria)->toArray();

于 2020-10-17T21:24:53.690 に答える