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

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

问题描述

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

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 ...

通常(不考虑 Doctrine)我认为数据库应该是这样的:
用户(ID、电子邮件、密码、facebook_id、角色)
公司(id、user_id、姓名、城市...)
客户(id、user_id、姓名、性别...)
消息(id, sender_id(user_id), receiver_id(user_id), message, ...)
...

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, ...)
...

所以现在我想知道使用 DoctrineSymfony 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)

(Note: I'm not using 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
{
    /**
     * @ORMcolumn(type="string")
     */
    protected password;

    /**
     * @ORMcolumn(type="array")
     */ 
    protected roles;

    /**
     * @ORMOneToOne(targetEntity="Company", mappedBy="user")
     */
    protected Company;

    /**
     * @ORMOneToOne(targetEntity="Client", mappedBy="user")
     */ 
    protected Client;
}

公司

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

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

客户

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

    /**
     * @ORMOneToOne(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
{
    /**
     * @ORMcolumn(type="string")
     */
    protected password;

    /**
     * @ORMcolumn(type="array")
     */ 
    protected roles;
}

公司

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

    /**
     * @ORMcolumn(type="string")
     */
    protected city;
}

客户

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

    /**
     * @ORMcolumn(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/Doctrine - 多用户类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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