如何通过学说查询生成器中的多对多关系从用户列表中获取关联数据

How do I fetch association data from a list of users through a many to many relationship in doctrine query builder

我不知道如何使用 Doctrine 中的查询构建器来做到这一点:

在高层次上,我想获取与每个用户关联的所有 userTag,并且我通过其父 ID 从用户关系 table 中获取每个用户。

像这样: from UserRelationship -> get users by parent_id -> also get each user's associated userTagsuserTags 是最后一点了。理想情况下,我会 select 只有 table 中的 idname 参数,因为其余的,比如公司和用户是多余的。

我在 Symfony 3.* Doctrine 2.*

中有这个查询生成器查询

UserRelationshipRepository.php

protected function getBaseSubordinateQuery($managerId ){

    $qb = $this->createQueryBuilder('r');

    $qb
        ->select(
            'u.id',
            'u.username',
            'u.email',
            'u.first_name',
            'u.last_name',
            'u.lastLogin',
            'u.userTags', // <-- How do I get these values?
            'c.phone',
            'c.position as jobTitle',
            'co.name as company',
            'c.createdAt',
            'max(cl.expiresAt) as expiresAt',
            'cl.expiresAt',
            'c.yearsInPosition as titleSince',
            'c.skype',
            'c.linkedin',
            'c.yearsAtCompany',
            'up.path as imgPath',
            'up.name as imgName',
            'up.format as imgFormat'
        )
        ->innerJoin( 'r.child', 'u' )
        ->innerJoin( 'u.clientInfo', 'c' )
        ->leftJoin( 'u.userTags', 'ut' )
        ->where('ut IS NOT NULL')
        ->leftJoin( 'u.company', 'co' )
        ->leftJoin( 'c.clientLicense', 'cl' )
        ->leftJoin( 'u.userPhotos', 'up' )
        ->where ( $qb->expr()->eq('r.parent', ':manager') )
        ->setParameter('manager', $managerId)
    ;

    return $qb;

注意:事实上,该查询给我一个错误:500 Internal Server Error. [Semantical Error] line 0, col 76 near 'userTags, c.phone,': Error: Invalid PathExpression. Must be a StateFieldPathExpression.

User.php

    /**
 * @var \Doctrine\Common\Collections\Collection|\Reddin\Bundle\CMSBundle\Entity\UserTag[]
 *
 * @ORM\ManyToMany(targetEntity="Reddin\Bundle\CMSBundle\Entity\UserTag", inversedBy="users")
 * @ORM\JoinTable(name="users_user_tags")
 *
 * @JMS\Groups({"tags"})
 * @JMS\MaxDepth(2)
 */
protected $userTags;

UserTag.php

class UserTag
{
    use Timestampable;

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * Many UserTags belong to one Company.
     *
     * @ORM\ManyToOne(targetEntity="Reddin\Bundle\CMSBundle\Entity\Company", inversedBy="userTags", fetch="EXTRA_LAZY")
     * @ORM\JoinColumn(name="company_id", referencedColumnName="id")
     * @JMS\MaxDepth(2)
     */
    private $company;

    /**
     * @var \Doctrine\Common\Collections\Collection|\Reddin\Bundle\UserAccountBundle\Entity\User[]
     *
     * @ORM\ManyToMany(targetEntity="Reddin\Bundle\UserAccountBundle\Entity\User", mappedBy="userTags", fetch="EXTRA_LAZY")
     * @JMS\MaxDepth(2)
     */
    private $users;

    /**
     * Company constructor.
     */
    public function __construct()
    {
        $this->users = new ArrayCollection();
    }

    public function __toString()
    {
        return "" . $this->getName();
    }

    /**
     * Get id
     *
     * @return int
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return UserTag
     */
    public function setName($name) {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName() {
        return $this->name;
    }

    /**
     * @return ArrayCollection|User[]
     */
    public function getUsers()
    {
        return $this->users;
    }

    /**
     * @return UserTag
     */
    public function getCompany() {
        return $this->company;
    }

    /**
     * @param Company $company
     * @return UserTag
     */
    public function setCompany(Company $company = null) {
        $this->company = $company;

        return $this;
    }
}

替换

'u.userTags',

'ut'

这将解决您遇到的错误,但您可能会遇到其他错误。 我建议将查询更改为:

$qb
    ->select(
        'r',
        'u',
        'ut',
        ...

也将第二个where替换为: ->andWhere ( $qb->expr()->eq('r.parent', ':manager') )

然后您可以获取每个用户的标签:

 $userRel->getChild()->getUserTags();