我该如何在教义中的多对多关系上执行findBy?

问题描述 投票:0回答:1

我有一个如下实体:

<?php
/** @Entity */
class User
{
    // ...

    /**
     * Many Users have Many Groups.
     * @ManyToMany(targetEntity="Group", inversedBy="users")
     * @JoinTable(name="users_groups")
     */
    private $groups;

    public function __construct() {
        $this->groups = new \Doctrine\Common\Collections\ArrayCollection();
    }

    // ...
}

/** @Entity */
class Group
{
    // ...
    /**
     * Many Groups have Many Users.
     * @ManyToMany(targetEntity="User", mappedBy="groups")
     */
    private $users;

    public function __construct() {
        $this->users = new \Doctrine\Common\Collections\ArrayCollection();
    }

    // ...
}

https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#many-to-many-bidirectional

我有与这些实体相关的存储库。如果存在一对多的关系,则很容易获得相关对象,因为您可以轻松执行以下操作:

/** @var RelatedEntity $relatedEntity*/
$this->findBy('relatedObject'=>$relatedEntity);

[当我尝试在上面显示的实体上执行它时,而不是彻底收集对象,让我们说说user_group表,它试图在user表中找到user_group列。它生成错误,例如:

[在执行'SELECT t0.id AS id_1,t0.created_at AS created_at_2,t0.updated_at AS Updated_at_3,t0.title AS title_4,t0.text AS text_5 FROM user t0时发生异常,WHERE user_group.group_id =吗?带有参数[“ 8192ed63-5d07-4c99-8902-006a3aa986e7”]:

SQLSTATE [42P01]:未定义表:7错误:表“ user_group”的FROM子句条目缺失]

我不知道应该如何正确完成。我错过了什么?

php doctrine-orm doctrine doctrine-query
1个回答
0
投票

对于ManyToMany,您可以在存储库和用户WHERE MEMBER OF中创建函数,这是一个示例

->andWhere(':activity MEMBER OF t.activities')
->setParameter('activity', $data['activity'])
© www.soinside.com 2019 - 2024. All rights reserved.