【问题标题】:Symfony/Doctrine relations good practiceSymfony/Doctrine 关系良好实践
【发布时间】:2019-06-18 07:36:18
【问题描述】:

我有实体UserLocation - 类似于商店,我需要添加它们之间的关系,但我不知道这有什么好的做法,因为:

用户可以有 3 种类型:经理、主管或员工,并且:

  • 每个位置都有一个用户作为经理,一个经理有一个位置;

  • 每个位置都有一个用户作为主管,一个主管有多个位置;

  • 每个位置都有很多用户作为员工,一个员工有一个位置;

首先想到的是给用户添加关系:

     /**
     * @ORM\OneToMany(targetEntity="App\Entity\Location", mappedBy="supervisor")
     */
    private $locations;

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\Location", mappedBy="manager")
     */
    private $location;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Location", inversedBy="employees")
     */
    private $wrokLocation;

地点:

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="locations")
     */
    private $supervisor;

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\User", inversedBy="location")
     */
    private $manager;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="wrokLocation")
     */
    private $employees;

但我不知道这是不是最好的方法。

【问题讨论】:

    标签: symfony doctrine symfony4


    【解决方案1】:

    个人我会做一个抽象类 User,然后是 3 个扩展 User 类的 Supervisor/Manager/Employee 类。与 Location 的关系将在每个类中,而不是在 User 类中。

    然后您可以在 Location 中定位这些类。

    【讨论】:

      猜你喜欢
      • 2020-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多