简体   繁体   English

教义2.0:如何在OneToOne关系中定位实体的超类

[英]Doctrine 2.0: How to target an entity's superclass in a OneToOne relationship

I am not very good at asking questions but the code below should be self-explanatory. 我不太会问问题,但是下面的代码应该是不言自明的。 I need to create a OneToOne association from a class to an entity's superclass which is NOT an entity. 我需要创建一个从类到不是实体的实体的超类的OneToOne关联。

/* Not an entity */
class Superclass {
      /** 
       *@Id @Column(name="entity_id", type="integer") @GeneratedValue 
       **/
      protected $id;
}

/**
 * @Entity @Table(name="subclasses1")
 **/
class Subclass1 extends Superclass {

}

/**
 * @Entity @Table(name="subclasses2")
 **/
class Subclass2 extends Superclass {

}


/**
 * @Entity @Table(name="assoc")
 **/
class Associationclass
{
    /**
     *OneToOne(targetEntity="Superclass")
     **/
    protected $association;

    /**
     *@Column(type="string")
     **/
    protected $info;
}

The question is: How can I reference both subclass1 and subclass2 using the OneToOne relationship without making Superclass an Entity (creating a new table and using discriminators)? 问题是:如何在不使超类成为实体的情况下(创建新表并使用区分符)使用OneToOne关系引用子类1和子类2?

You can't. 你不能 If you want that sort of inheritance (the kind you can use in associations), you need to model the inheritance in doctrine. 如果您想要这种继承(可以在关联中使用的那种),则需要在学说中对继承建模。

The association needs a "targetEntity" -- which, like the name indicates, must be an entity. 关联需要一个“ targetEntity”-就像名称所示,它必须是一个实体。

Unless there's a very good reason not to, go ahead and make your superclass an entity, and set up the inheritance in a way doctrine can understand. 除非有很好的理由,否则,继续使您的超类成为一个实体,并以一种学说可以理解的方式建立继承。

The reason your superclass needs to be an entity is because the superclass and it's subclasses will then share an identifier. 您的超类需要成为一个实体的原因是因为该超类及其子类将共享一个标识符。 So with the identifier (and the discriminator), doctrine can then figure out that SuperClass#1234 is actually a SubClass2. 因此,有了标识符(和区分符),该学说便可以确定SuperClass#1234实际上是SubClass2。

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

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