简体   繁体   English

Doctrine2类表继承

[英]Doctrine2 Class Table Inheritance

I am a little bit confused by Doctrine's documentation so maybe you can help me. 我对Doctrine的文档有些困惑,所以也许您可以帮我。 I have the following class inheritance: 我具有以下类继承:

<?php

class User
{
   /**
    * @var int
    */
    private $_id;

   /**
    * @var Role
    */
    private $_role;
}

class Company extends User
{

}

class Customer extends User
{
    ...
}

class Role
{
   /**
    * @var int
    */
    private $_id;
}

?>

I want to store each class in a separate table. 我想将每个类存储在单独的表中。 The role defines the type of user by an id. 角色通过ID定义用户的类型。 How would I solve this problem? 我该如何解决这个问题? I tried this: 我尝试了这个:

<?php

/**
 * @Entity
 * @Table(name="user")
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="role_id", type="integer")
 * @DiscriminatorMap({"1" = "User", "2" = "Customer"})
 */
class User
{
    ...
}

?>

I am not sure how to handle the role class in this scenario. 我不确定在这种情况下如何处理角色类。


Thank you for your answer. 谢谢您的回答。 Now I tried this and got following error: 现在我尝试了这个,并得到以下错误:

[Doctrine\DBAL\Schema\SchemaException]
There is no column with name '_id' on table 'customer'.

I have following code: 我有以下代码:

<?php

/**
 * Class Sb_User
 *
 * @Entity
 * @Table(name="user")
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="role_id", type="integer")
 * @DiscriminatorMap({"2" = "Sb_Customer", "8" = "Sb_Pos"})
 */
class Sb_User implements Sb_User_Interface
{
    /**
     * @Id
     * @GeneratedValue
     * @Column(name="id", type="integer")
     * @var int
     */
     protected $_id;

     ...
}

/**
 * Class Sb_Customer
 *
 * @Entity
 * @EntityResult(discriminatorColumn="role_id")
 * @Table(name="customer")
 *
 */
class Sb_Customer extends Sb_User implements Sb_Customer_Interface
{
    ....
}

I do not know what I am doing wrong. 我不知道我在做什么错。 Can you help me? 你能帮助我吗?

?>

Your question is a bit confusing to me. 您的问题让我有些困惑。

You want to use joined table inheritance to define the role of a user, but you also want to add a role attribute to your user that, as I understand, does exactly the same. 您想使用联接表继承来定义用户的角色,但是您还想向您的用户添加一个Role属性,据我所知,该属性的作用完全相同。 Seems like you're trying to do the same thing twice in a different way. 似乎您正在尝试以不同的方式做同一件事两次。

Anyway, I will try to give you an answer. 无论如何,我会尽力给你一个答案。

If you want to use separate tables for each type (Customer, Company, etc) you should have a look at mapped superclasses: http://docs.doctrine-project.org/en/2.0.x/reference/inheritance-mapping.html#mapped-superclasses 如果要为每种类型(客户,公司等)使用单独的表,则应查看映射的超类: http : //docs.doctrine-project.org/en/2.0.x/reference/inheritance-mapping。 html#mapped-superclasses

That way you can define basic class attributes and relations that will be used by all the entities that extend it. 这样,您可以定义所有扩展它的实体都将使用的基本类属性和关系。 All data will be saved in separate tables for each entity. 每个实体的所有数据将保存在单独的表中。

If you want to define the role of a user using the Role entity you should define a many-to-one relation between user and role. 如果要使用“角色”实体定义用户的角色,则应定义用户和角色之间的多对一关系。

Good luck! 祝好运!

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

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