2012-08-22 2 views
3

내 웹 사이트의 일부로 Symfony2와 Doctrine2를 사용하여 태깅 (folksonomy) 시스템을 만들려고합니다.Doctrine2 Symfony2 innerJoin QueryException 예상되는 Doctrine ORM Query Lexer :: T_WITH, '켜짐'

내가 아래에있는 내 교리 엔티티 작성하는 문서에서 테이블 및 쿼리의 예를 다음 해요 : 나는 교리 쿼리 작성기 쿼리 (문서에 주어진) MySQL의 쿼리를 변환 할 때 http://dablog.ulcc.ac.uk/wp-content/uploads/2007/12/tagging_folksonomy.pdf

을 나는 오류를 얻을 안쪽 조인즈와. 예 아래 : 문서에서

MySQL의 쿼리

SELECT tag_text 
, COUNT(*) as num_tags 
FROM Tag2Post t2p 
INNER JOIN Tags t 
ON t2p.tag_id = t.tag_id 
GROUP BY tag_text; 

내 교리 쿼리 빌더 쿼리 :

$qb = $em->createQueryBuilder() 
      ->select('t.tag_text, COUNT(*) as num_tags') 
      ->from('CompanyWebsiteBundle:Tag2Post', 't2p') 
      ->innerJoin('CompanyWebsiteBundle:Tags', 't', 'ON', 't2p.tag_id = t.id') 
      ->groupBy('t.tag_text') 
; 
$tags = $qb->getQuery()->getResult(); 

오류 메시지 :

[2/2] QueryException: [Syntax Error] line 0, col 112: Error: Expected Doctrine\ORM\Query\Lexer::T_WITH, got 'ON' 
[1/2] QueryException: SELECT t.tag_text, COUNT(*) as num_tags FROM CompanyWebsiteBundle:Tag2Post t2p INNER JOIN CompanyWebsiteBundle:Tag t ON t2p.tag_id = t.id GROUP BY t.tag_text 

내가 직접 MySQL의 쿼리를 실행할 때 데이터베이스에서 작동합니다!

답변

10

이렇게하면됩니다. DQL에서 ON 키워드는 WITH으로 바뀝니다. 제대로 엔티티를 구성한 경우

$qb = $em->createQueryBuilder() 
     ->select('t.tag_text, COUNT(*) as num_tags') 
     ->from('CompanyWebsiteBundle:Tag2Post', 't2p') 
     ->innerJoin('CompanyWebsiteBundle:Tags', 't', 'WITH', 't2p.tag_id = t.id') 
     ->groupBy('t.tag_text') 
; 
$tags = $qb->getQuery()->getResult(); 

또한, 자동으로 관계를 찾아야한다 교리와 , 'WITH', 't2p.tag_id = t.id' 일부를 생략 할 수 있어야한다. 예를 들어

:

$qb = $em->createQueryBuilder() 
     ->select('t.tag_text, COUNT(*) as num_tags') 
     ->from('CompanyWebsiteBundle:Tag2Post', 't2p') 
     ->innerJoin('t2p.tags', 't') 
     ->groupBy('t.tag_text') 
; 
$tags = $qb->getQuery()->getResult(); 
+0

많은 감사 smottt :) 사이드 참고로 지금은 'num_tags으로 *)''[의미 론적 오류] 라인 0, COL (21) 근처에 다음과 같은 오류를 받고 있어요 : 오류 : '*'는 정의되어 있지 않습니다. Doctrine2에서 어떻게'count (*)'를 할 수 있습니까? – mattvick

+0

계산할 항목을 정의해야합니다. t2p. * 또는 t. * 또는 다른 것. – smottt