ZF2 +教义2-具有类表继承的子级区分符 [英] ZF2 + Doctrine 2 - Child-level discriminators with Class Table Inheritance

查看:73
本文介绍了ZF2 +教义2-具有类表继承的子级区分符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SO和Web上关于ZF2和Doctrine 2并使用Discriminators的很多问题是:如何不在父实体上声明所有子实体?尤其是当您有多个模块时?

Much asked on SO and around the web with regards to ZF2 with Doctrine 2 and using Discriminators is: how do you not declare all child Entities on the parent Entity? Especially when you have multiple modules?

简短的答案是:请勿声明discriminatorMap.教义将为您处理.

The short answer is: do not declare a discriminatorMap. Doctrine will handle it for you.

更长的答案在下面.

推荐答案

关于如何能够在子实体(而不是父实体)上声明子实体(而不是父实体)的热门文章,

A popular article on how to be able to declare your child Entities, on the children Entities, instead of the parent, is this one.

但是,《 Doctrine 2》自编写以来已有一些变化,例如AnnotationWriter不再存在.

However, Doctrine 2 has changed somewhat since it was written, e.g. the AnnotationWriter no longer exists.

但是,有一种更简单的方法,正如我在问题中提到的:什么也不做.

There, however, is a simpler way, as I mentioned in the question: do nothing.

现在要使用"类表继承"方法(与单表继承"相对)是要不要对鉴别符映射进行声明! (不确定这是否也适用于STI ...)

To now use Discriminators using the "Class Table Inheritance" method (as opposed to "Single Table Inheritance") is to NOT DECLARE a Discriminator Map! (Not sure if this will also work for STI…)

我在Github上发现了一张旧票 ,该票解释了与该答案相同的问题而且还有很多人认为,向父母宣告是没有意义的.阅读完这些内容后,我会深入研究代码并仔细阅读文档.

I found an old ticket on Github that explains the same issue as this answer and which many people still have, that declaring on the parent makes no sense. After reading through that, I dove into the code and re-read the docs, carefully.

此外,如果您在阅读文档时特别小心,则可以通过不说来做到这一点.

Also, if you’re reeeeaaally careful when reading the docs, it says this is possible, by not saying it.

报价:

注意事项:

Things to note:

必须在作为映射实体层次结构一部分的最顶层类上指定@ InheritanceType,@ DiscriminatorColumn和@DiscriminatorMap.

The @InheritanceType, @DiscriminatorColumn and @DiscriminatorMap must be specified on the topmost class that is part of the mapped entity hierarchy.

@DiscriminatorMap指定鉴别符列的哪些值将某行标识为哪种类型.在上述情况下,"person"值将一行标识为Person类型,而"employee"值将一行标识为Employee类型.

The @DiscriminatorMap specifies which values of the discriminator column identify a row as being of which type. In the case above a value of "person" identifies a row as being of type Person and "employee" identifies a row as being of type Employee.

如果分类器类中的类名称与应用区分器图的实体类位于同一名称空间中,则不需要完全限定这些类的名称.

The names of the classes in the discriminator map do not need to be fully qualified if the classes are contained in the same namespace as the entity class on which the discriminator map is applied.

如果未提供鉴别符映射,则该映射将自动生成.自动生成的鉴别符映射将每个类的小写短名称作为键.

If no discriminator map is provided, then the map is generated automatically. The automatically generated discriminator map contains the lowercase short name of each class as key.

当然,以上文档确实明确声明如果未提供地图,则将生成.尽管它与第一个要注意的东西相矛盾,这是必须在层次结构中最顶层的类上提供@DiscriminatorMap.

Of course, the above piece of documentation does explicitly state that a map would be generated if none was provided. Though it contradicts the first thing to note, which is that the @DiscriminatorMap must be provided on the topmost class in the hierarchy.

因此,如果您要跨几个模块扩展类(因为我认为这就是您要阅读的原因),请不要声明鉴别器图!

So, if you were to stretch your classes across several modules (as I assume that’s why you would be reading this), do not declare a discriminator map!

下面我将举一个例子:

<?php
namespace My\Namespace\Entity;

/**
 * @Entity
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="discr", type="string")
 * // NOTE: No @DiscriminatorMap!!!
 */
class Person
{
    // ...
}


<?php
namespace My\Other\Namespace\Entity;

/** @Entity */
class Employee extends \My\Namespace\Entity\Person
{
    // ...
}

使用doctrine CLI命令检查实体时,您会发现这是正确的.

When you use the doctrine CLI command to check your entities, you’ll find this is correct.

此外,通过使用实体检查命令来检查其是否完全正常工作:

Also, check that it fully works by using the entity checking command:

./vendor/bin/doctrine-module orm:mapping:describe "\My\Namespace\Entity\Person"

该命令的响应顶部附近将是以下行:

Near the top of the response of that command will be this line:

| Discriminator map | {"person":"My\\Namespace\\Entity\\Person","employee":"My\\Other\\Namespace\\Entity\\Employee"}

这篇关于ZF2 +教义2-具有类表继承的子级区分符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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