如何在条令中的多对多关系上执行findBy



我有一个如下实体:

<?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 DoctrineCommonCollectionsArrayCollection();
}
// ...
}
/** @Entity */
class Group
{
// ...
/**
* Many Groups have Many Users.
* @ManyToMany(targetEntity="User", mappedBy="groups")
*/
private $users;
public function __construct() {
$this->users = new DoctrineCommonCollectionsArrayCollection();
}
// ...
}

https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#many-到许多双向

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

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

当我尝试在上面显示的实体上执行它时,而不是完全收集对象(比如user_group表(,它试图在用户表中找到user_groups列。它会产生错误,如:

执行"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子句条目

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

在ManyToMany的情况下,您可以在存储库和用户WHERE MEMBER of中创建函数,下面是一个示例

->andWhere(':activity MEMBER OF t.activities')
->setParameter('activity', $data['activity'])

相关内容

  • 没有找到相关文章

最新更新