0

我有 2 张桌子:commentuser

我有以comment下列:id, user_id, conversation_id, body...

并且user我有以下列:idusername...

我想用学说执行以下查询:

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

换句话说,当我获得评论列表时,我想显示表格中name的内容。user

我知道如何在 SQL 中做到这一点,但我无法在教义中做到这一点。

它应该看起来像这样:

$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: {  }

谢谢!

4

1 回答 1

0

您必须从 CommentRepository 调用请求,它必须看起来像

$qb = $this->createQueryBuilder('c');
$qb->select('c, u.username')
   ->leftJoin('c.userId', 'u')
   ->where($qb->expr()->eq('c.conversationId', $conversationId));
$qb->getQuery();
于 2013-10-14T11:30:12.060 回答