简体   繁体   English

与类型 Symfony Doctrine 的多对多关系

[英]ManyToMany Relationship With Type Symfony Doctrine

I have this structure of Entities我有这种实体结构

class Group
{
    /**
     * @var int
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private int $id;

    /**
     * @var string
     * @ORM\Column(name="name", type="string", length=50, nullable=false)
     */
    private string $name;
}


class GroupUser
{
    /**
     * @var int
     *
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private int $id;

    /**
     * @var Group
     * @ORM\ManyToOne(targetEntity="Group", inversedBy="GroupUser")
     * @ORM\JoinColumn(name="group_id", referencedColumnName="id", nullable=false)
     */
    private Group $group;

    /**
     * @var string
     * @ORM\Column(type="string", length=50, nullable=false)
     */
    private string $type;

    /**
     * @var int
     * @ORM\Column(type="integer", nullable=false)
     */
    private int $user;

}

And there are two types of users.并且有两种类型的用户。 Admins and Clients.管理员和客户。 There's a ManyToMany relationship between Group and User.组和用户之间存在多对多关系。 And the property in GroupUser $type is saving the Class of either Admin or Client and the property $user is saving the id. GroupUser $type 中的属性正在保存管理员或客户端的 Class,而 $user 属性正在保存 id。

id ID group_id group_id user用户 type类型
1 1 1 1 1 1 Entity\Admin实体\管理员
2 2 2 2 1 1 Entity\Client实体\客户

How do I join it using doctrine from the Admin and Client-side?如何从管理员和客户端使用 doctrine 加入它? Or maybe someone could point out to some resources how this kind of relationship works on doctrine?或者也许有人可以指出一些资源,这种关系如何在 doctrine 上起作用? As I'm having a hard time googling anything out.因为我很难用谷歌搜索任何东西。 I imagine it could be like a conditional leftJoin, but I can't seem to figure it out.我想它可能就像一个有条件的leftJoin,但我似乎无法弄清楚。

Normaly, you cannot do that on database, because it not safe.通常,您不能在数据库上执行此操作,因为它不安全。 Why is it not safe?为什么不安全? because you can have an id in the user column of userGroup table that refer to nothing as it is not linked.因为您可以在userGroup表的user列中有一个id ,因为它没有链接,所以它没有引用任何内容。

I will write what you should have done, and how you can achieve what you want using your own method:我会写出你应该做的事情,以及如何使用自己的方法实现你想要的:

What you should have done: In your UserGroup entities, have 2 columns ( admin and client ) which are linked to the related entities.您应该做什么:在您的UserGroup实体中,有 2 列( adminclient )链接到相关实体。 They can be null ( Client is null and admin contain the id of admin entity if it is an admin and vice versa).它们可以是 null( Client是 null,如果是管理员,则admin包含admin实体的 ID,反之亦然)。 Then you can delete the type column然后你可以删除type

How to achieve what you using your method: As it cannot be done in the database, you will have to do it in some manager.如何使用您的方法实现您的目标:由于无法在数据库中完成,您必须在某个管理器中完成。 Have a method getUser which will check on your type attribute and return the associated entity from the current id stored in $user有一个方法getUser它将检查您的type属性并从存储在$user中的当前 id 返回关联的实体

example:例子:

    public function getUserFromGroupUser(GroupUser $groupUser){
        if('Entity\Admin' ===$groupUser->getType()){
            return $this->adminRepository->find($groupUser->getUser());
        }
        if('Entity\Client' ===$groupUser->getType()){
            return $this->ClientRepository->find($groupUser->getUser());
        }
        throw new \RuntimeException('the type does not exist');
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM