developer tip

Doctrine2 쿼리에서 제한 및 오프셋 사용

copycodes 2021. 1. 9. 10:13
반응형

Doctrine2 쿼리에서 제한 및 오프셋 사용


페이지 매김을 시도하고 있지만 오류가 있습니다.

[구문 오류] 줄 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); 

나는 그것이 어리 석고 심지어 잘못되었다는 것을 알고 있지만 (특히 세 번째 매개 변수 $page*$FR_PER_PAGE는) 쿼리가 작동하는지 시도하고 싶었지만 실패했습니다.


아니. 사용하다:

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

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

당신이 사용할 수있는 findBy3,있는 교리 저장소의 방법의 4 매개 변수 limitoffset.

다음은 메서드 정의입니다.

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


당신은 또한 사용할 수 있습니다

$query->getSingleResult();

ReferenceURL : https://stackoverflow.com/questions/12198046/use-limit-and-offset-in-doctrine2-query

반응형