Symfony/主义-多种用户类型 [英] Symfony / Doctrine - Multiple Users Types

查看:60
本文介绍了Symfony/主义-多种用户类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个网站,该网站应至少具有两种类型的用户,即Company和Client,其中两个具有相同的登录表单和不同的注册表单,并且它们可以彼此发送消息...

I'm creating a website that should have at least two types of users, Company and Client, two of them have the same login form and different registration forms, and they can send messages to each other ...

通常(不用考虑教义),我认为数据库应该看起来像这样:
用户(ID,电子邮件,密码,facebook_id,角色)
公司(ID,用户ID,名称,城市,...)
客户(id,user_id,姓名,性别...)
消息(ID,发送方ID(用户ID),接收方ID(用户ID),消息...)
...

Normaly (Without think about Doctrine) I think the database should look something like that:
User (id, email, password, facebook_id, roles)
Company(id, user_id, name, city, ...)
Client(id, user_id, name, sex ...)
Messages(id, sender_id(user_id), receiver_id(user_id), message, ...)
...

因此,现在我想了解使用 Doctrine Symfony 4 来实现此要求的最佳和最简单的方法,实体的外观如何?

So now I want to know the best and the simplest way to implement this requirements using Doctrine and Symfony 4, How Entities could look like ?

(注意:我没有使用 FOSUserBundle )

推荐答案

实施此域:

  • 用户
  • 公司
  • 客户

您可以考虑两种不同的方法:

you may consider two different approaches:

此方法使用关联.如果选择此选项,则应根据某些逻辑手动将适当的(公司或客户)链接到当前用户.在任何给定的时间点,每个 User 可能只应具有这些字段之一.公司或客户,而不是两者兼有.

This approach uses association. If you choose this one, you should manually link appropriate (company or client) to the current user depending on some logic. Each User should probably have only one of those field at any given point in time. Company or Client, not both.

用户

/** @Entity */
class User
{
    /**
     * @ORM\column(type="string")
     */
    protected password;

    /**
     * @ORM\column(type="array")
     */ 
    protected roles;

    /**
     * @ORM\OneToOne(targetEntity="Company", mappedBy="user")
     */
    protected Company;

    /**
     * @ORM\OneToOne(targetEntity="Client", mappedBy="user")
     */ 
    protected Client;
}

公司

/** @Entity */
class Company
{
    /**
     * @ORM\column(type="string")
     */
    protected city;

    /**
     * @ORM\OneToOne(targetEntity="User", inversedBy="company")
     */ 
    protected user;
}

客户

/** @Entity */
class Client
{
    /**
     * @ORM\column(type="string")
     */
    protected sex;

    /**
     * @ORM\OneToOne(targetEntity="User", inversedBy="client")
     */ 
    protected user;
}

第二种方法

此方法使用继承,并且似乎更灵活,但也有其缺点.

Second approach

This approach uses inheritance and seems to be more flexible, but has its own downsides.

用户

/** @MappedSuperclass */
class User
{
    /**
     * @ORM\column(type="string")
     */
    protected password;

    /**
     * @ORM\column(type="array")
     */ 
    protected roles;
}

公司

/** @Entity */
class Company extends User
{
    /**
     * @Id @Column(type="integer")
     */
    protected $id;

    /**
     * @ORM\column(type="string")
     */
    protected city;
}

客户

/** @Entity */
class Client extends User
{
    /**
     * @Id @Column(type="integer")
     */
    protected $id;

    /**
     * @ORM\column(type="string")
     */
    protected sex;
}

您在用户和&消息:

  • 一个 用户可以具有许多 消息
  • 一个 消息仅属于一个 用户
  • One User can have Many Messages
  • One Message belongs to only One User

使用上面的第一种方法很好,但是使用第二种方法,您会遇到

Using First approach above is fine, but using the Second approach, you are getting yourself in trouble as Doctrine says:

映射超类定义的

持久关系必须是单向(仅具有拥有方).这意味着在映射的超类上根本不可能一对多关联.

persistent relationships defined by a mapped superclass must be unidirectional (with an owning side only). This means that One-To-Many associations are not possible on a mapped superclass at all.

这篇关于Symfony/主义-多种用户类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆