【问题标题】:doctrine - single table inheritance in a single row学说 - 单行中的单表继承
【发布时间】:2014-12-28 11:23:55
【问题描述】:

我想做的是创建一个人可以拥有的角色层次。并使用 Doctrine 和 Symfony2 来做到这一点。

层次结构的顶部是。 person 有一个子类,称为employee,另外两个子类称为it_usertelephony_user。每一个都为基类 person 添加了几个属性。

如果我理解正确,单表继承对于同一个人,我会有不同的行(一个按角色/子类)。

我想要同一个人的同一行,并管理每个类/子类的某个属性子集。

问题:

单表继承是一种很好的方法吗?有什么调整吗?

或者其他更好的经典方法是使用名为 person 的超类,聚合所有角色的所有属性?

还有其他更好的方法吗?

提前谢谢你。

【问题讨论】:

    标签: symfony inheritance doctrine-orm


    【解决方案1】:

    如果你打算根据人物类型设置不同的属性,那么你应该使用 Class Table Inheritance。这种继承使您可以进行以下操作:

    超类:

    Person
    - name
    - gender
    

    子类 1:

    Employ extends Person
    - manager
    - projects
    

    子类 2:

    TelephonyUser extends Person
    - schedule
    - foo
    - bar
    

    通过这种方式,每个模型(子类)都映射到它的表。了解每个子类如何相互拥有自定义属性并同时共享父属性。

    单表继承 (STI) 和聚合

    看看下面的数据库架构:

    -- Uploads table
    id  |  name    | size   |  type  |  filename 
    ------------------------------------------ 
    1   | upload1  | 43423  |  image | foo.gif
    2   | upload2  | 64234  |  file  | hack.txt
    3   | upload3  | 76646  |  image | bar.gif
    

    文件图片上传类的两个子类,根据类型列。如您所见,它们在处理子类类型方面具有相同的属性。为什么?因为这种继承是明智的基于行为模型而不是结构。例如,您可以为每个类以不同的方式管理属性数据:

    // In file class the content as text should be showen
    class File extends Uploads{
    
        // show the file content
        public function show(){
            echo file_get_contents( $this->filename );
        }
    }
    
    // In Image class the image should be showen
    class File extends Uploads{
    
        // show the file content
        public function show(){
            echo "<img src='{ $this->filename }' alt='' />";
        }
    }
    

    查看每个具有相同属性的类,它们管理它们的方式是如何不同的。但这里最相关的是所有数据都将存储在unique table 中。因此,您需要做的是为您的 db squema 建模,以便应用适当的继承。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-14
      • 1970-01-01
      • 2010-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多