【问题标题】:How do I fetch association data from a list of users through a many to many relationship in doctrine query builder如何通过原则查询构建器中的多对多关系从用户列表中获取关联数据
【发布时间】:2017-12-13 22:31:47
【问题描述】:

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

在高层次上,我想获取与每个用户相关联的所有 userTags,并且我通过他们的父 ID 从用户关系表中获取每个用户。

类似这样的: from UserRelationship -> get users by parent_id -> also get each user's associated userTags。这是我无法获得userTags 的最后一点。理想情况下,我会从该表中仅选择 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.

用户.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;

用户标签.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;
    }
}

【问题讨论】:

    标签: mysql doctrine-orm doctrine many-to-many query-builder


    【解决方案1】:

    替换

    'u.userTags',
    

    'ut'
    

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

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

    还要替换第二个位置: ->andWhere ($qb->expr()->eq('r.parent', ':manager'))

    然后你可以为每个用户获取标签:

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

    【讨论】:

    • 谢谢,我试图避免执行另一个查询来获取标签,并避免加载整个对象。最后,我切换到原生 SQL 查询,并且更容易和更直观地对其进行合理化。对于任何可能觉得有用的人,我都会接受这个答案。
    猜你喜欢
    • 2021-04-04
    • 1970-01-01
    • 2017-05-25
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 2020-10-04
    相关资源
    最近更新 更多