2013-10-14 2 views
0

나는이 개 테이블이 : commentcommentuser여러 엔티티로 Doctrine에서 다음 쿼리를 수행하는 방법은 무엇입니까?

나는 다음과 같은 열이 : id, user_id, conversation_id, body ...

user에 나는 다음과 같은 열이 있습니다 id, username을 ..

doctrine을 사용하여 다음 쿼리를 수행하고 싶습니다.

"SELECT c.*, u.username FROM comment c LEFT JOIN user u on c.user_id = u.id WHERE c.conversation_id = '5'" 

다른 말로하면 테이블에있는 name을 표시하고 싶은 의견 목록을 얻을 수 있습니다.

나는 SQL에서 그것을하는 방법을 알고 있지만 doctrine에서 그것을 할 수 없다.

그것은 다음과 비슷한 모습이 될 것입니다

$q = $this 
    ->createQueryBuilder('u') 
    ->where('u.conversationId = :conversationId') 
    ->setParameter('conversationId', $conversationId) 
    ->getQuery(); 

교리 코멘트 엔터티 :

Test\SocialBundle\Entity\Comment: 
type: entity 
table: comment 
repositoryClass: Test\SocialBundle\Entity\CommentRepository 
fields: 
    id: 
     type: integer 
     id: true 
     generator: 
      strategy: AUTO 
    conversationId: 
     type: integer 
     nullable: false 
     column: conversation_id 
    userId: 
     type: integer 
     nullable: false 
     column: user_id 
    body: 
     type: string 
     nullable: false 
     length: '300' 
lifecycleCallbacks: { } 

교리 사용자 엔터티 :

Test\UserBundle\Entity\User: 
type: entity 
table: user 
repositoryClass: Test\UserBundle\Entity\UserRepository 
fields: 
    id: 
     type: integer 
     id: true 
     generator: 
      strategy: AUTO 
    username: 
     type: string 
     length: '100' 
     nullable: true 
     column: username 
    name: 
     type: string 
     length: '100' 
     nullable: true 
     column: name 
manyToMany: 
    roles: 
     targetEntity: Role 
     joinTable: 
      name: user_role 
      joinColumns: 
       user_id: 
        referencedColumnName: id 
      inverseJoinColumns: 
       role_id: 
        referencedColumnName: id 
lifecycleCallbacks: { } 

감사합니다!

+0

것 같습니다. 당신은 외래 키들 대신에 객체쪽으로'ORM' 이동 논리를 사용하는 것을 안다. –

답변

0

당신은 CommentRepository에서 요청을 호출해야합니다, 그것은해야 교리의 실체 '구조를 게시하시기 바랍니다

$qb = $this->createQueryBuilder('c'); 
$qb->select('c, u.username') 
    ->leftJoin('c.userId', 'u') 
    ->where($qb->expr()->eq('c.conversationId', $conversationId)); 
$qb->getQuery(); 
관련 문제