2012-07-13 5 views
1

ID, 게임 (외래 키) 및 날짜가있는 테이블 설명에서 주석을 검색하려고합니다.Doctrine 2의 행 수

내가 의견을 요청할 때마다 지정된 게임에 대해 날짜별로 3 개의 의견을 정렬하고 나중에 표시 할 추가 의견이 있는지 알고 싶습니다. 이를 위해 두 가지 함수를 작성했습니다. 첫 번째 함수는 세 가지 주석을 반환합니다.

public function getRecentComments($offset,$id) { 
    $dql = "SELECT c FROM Comment c 
     WHERE c.game = ?1 
     ORDER BY c.date DESC"; 
    $query = $this->getEntityManager()-> 
     createQuery($dql)-> 
     setParameter(1, (int)$id)->  
     setMaxResults(3)-> 
     setFirstResult($offset); 
    return $query->getResult(); 

두 번째 함수는 나중에 얻을 수있는 주석의 개수를 반환합니다. 이 기능을 사용하는 이유는 "More comments"버튼을 보여주기 위함입니다. 이것은 두 번째 기능입니다 :

public function moreComments($offset,$id) { 

    $dql = "SELECT COUNT(c.id) FROM Comment c 
     WHERE c.game = ?1 
     ORDER BY c.date DESC"; 
    $query = $this->getEntityManager() 
     ->createQuery($dql) 
     ->setParameter(1, (int)$idPartido) 
     ->setFirstResult($offset+3)  
     ->setMaxResults(1) 
     ->getSingleScalarResult(); 

    return $query; 
} 

그러나 두 번째 기능은 다음 오류가 작동하지 않습니다 :

치명적인 오류 : catch되지 않은 예외 '교리 \ ORM \ NoResultException'결과가 없습니다 '메시지가에 대한 발견 적어도 하나의 행이 예상되었지만 쿼리.

나는 이것이 setFirstResult와 count()를 사용하기 때문이라고 생각한다.

그래서, 난 분명히 난 단지 카운트에 대한 데이터를 얻을 것이기 때문 작성 나쁜

public function moreComments($offset,$id) { 

    $dql = "SELECT c FROM Comentario c 
     WHERE c.partido = ?1 
     ORDER BY c.fecha DESC"; 
    $query = $this->getEntityManager() 
     ->createQuery($dql) 
     ->setParameter(1, (int)$idPartido) 
     ->setFirstResult($offset+3)  
     ->setMaxResults(1) 
     ->getSingleScalarResult(); 

    return sizeof($query); 
} 

을 사용했습니다. 어떻게 두 번째 함수를 올바르게 쓸 수 있습니까?

미리 감사드립니다.

답변

3

MySQL을 사용하는 경우에만 FOUND_ROWS() 기능을 이용할 수 있습니다.

네이티브 쿼리를 사용하면 MySQL 이외의 DB를 사용할 수있는 가능성이 높아지지만 내 경험상 꽤 잘 작동합니다.

다음과 같이 큰 성과를 거뒀습니다.

use Doctrine\ORM\Query\ResultSetMapping; 

public function getRecentComments($offset, $id) { 
    $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM Comment c 
     WHERE c.game = ? 
     ORDER BY c.date DESC 
     LIMIT ?,3"; 
    $rsm = new ResultSetMapping(); 
    $rsm->addEntityResult('Comment', 'c'); 
    $rsm->addFieldResult('c', 'id', 'id'); 
    $rsm->addFieldResult('c', 'game_id', 'game_id'); 
    $rsm->addFieldResult('c', 'date', 'date'); 
    $query = $this->getEntityManager()->createNativeQuery($dql, $rsm); 
    $query->setParameters(array(
     (int)$id, 
     (int)$offset 
    )); 
    $results = $query->getResult(); 

    // Run FOUND_ROWS query and add to results array 
    $sql = 'SELECT FOUND_ROWS() AS foundRows'; 
    $rsm = new ResultSetMapping(); 
    $rsm->addScalarResult('foundRows', 'foundRows'); 
    $query = $this->getEntityManager()->createNativeQuery($sql, $rsm); 
    $foundRows = $query->getResult(); 
    $results['foundRows'] = $foundRows[0]['foundRows']; 

    return $results; 
} 

위 함수의 결과 어레이를 얻고 난 후에, I는 별도의 변수에 'foundRows'요소를 추출 해제는 (즉, unset($results['foundRows']))하고 정상적으로 어레이를 계속.

희망이 도움이됩니다.

+0

정말 감사합니다. 그것은 나를 많이 도왔다. 그러나 나는 곧 postgresql을 사용할 것이다. – honnix

+2

기존의 두 번째 쿼리를 사용할 수 있다고 생각하지만'getSingleScalarResult()'를'getScalarResult()'로 대체하십시오. 또는 try/catch 블록에서 두 번째 쿼리를 래핑하여 예외를 catch하면 예외가 throw되면 더 이상 주석이 없다는 것을 알 수 있습니다. – rexmac

관련 문제