当一个子类没有额外的属性时,Doctrine表类继承 [英] Doctrine table class inheritance when one subclass has no extra attributes

查看:260
本文介绍了当一个子类没有额外的属性时,Doctrine表类继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的映射有问题。我不能让它上班我有一个这样的抽象基类:

  / ** 
* @Entity
* @Table (name =actions)
* @InheritanceType(JOINED)
* @DiscriminatorColumn(name =type,type =string)
* @DiscriminatorMap({FOO =FooAction,BAR=BarAction,...})
* /
抽象类AbstractAction
{
...
}

我有一堆不同的动作,全部都有不同的字段。例如:

  / ** 
* @Entity
* @Table(name =actions_foo)
* /
class FooAction extends AbstractAction
{
...
}

但是我的一个动作( BarAction )不需要任何额外的字段,除了 AbstractAction 。但是如何映射呢?我尝试省略 @Table ,或者使用相同的 @Table 作为 AbstractAction ,但不起作用。

  / ** 
* @Entity
* @Table(name =actions)
* /
class BarAction extends AbstractAction
{
...
}

省略 @Table 给我一个 PDOException 关于一个缺失的表 BarAction 。使用基础的 @Table 给我:

  PDOException: SQLSTATE [HY093]:参数号无效:绑定变量的数量不符合令牌数

所以,如何映射这个?



编辑:到目前为止,我还尝试了两件事。



BarAction @Entity 以及 @Table 希望以这种方式不再需要数据库表。那不行。相反,我得到这个错误:

  Doctrine\ORM\Mapping\MappingException:Class BarAction不是一个有效的实体或映射超类。 

接下来我试图创建一个 actions_bar 我的数据库只有一个外键列 id 。然后我将 BarAction 映射到它。这样做(yay!),但是感觉到crufty和丑陋有一个额外的SQL表,我根本不需要。



所以,仍然寻找一个更好的方式...

解决方案

你正在使用加入继承模型继承),它为父和每个子使用一个单独的表。如果您没有在子类中指定任何字段,Doctrine将只创建一个仅包含ID字段的表。



父类只能使用一种类型的继承,类表继承或单表继承。



在这种情况下,如果您不希望有一个只有id列的表,则需要更改您的数据模型。


I'm having a problem with my mapping. I can't get it to work. I have an abstract base class like so:

/**
 * @Entity
 * @Table(name="actions")
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="type", type="string")
 * @DiscriminatorMap({"FOO" = "FooAction", "BAR" = "BarAction", ...})
 */
abstract class AbstractAction
{
    ...
}

I have a bunch of different actions, all with different fields. E.g:

/**
 * @Entity
 * @Table(name="actions_foo")
 */
class FooAction extends AbstractAction
{
   ...
}

But one of my actions (BarAction) does not need any extra fields besides those supplied by the AbstractAction. But how can I map that? I tried omitting the @Table, or using the same @Table as the AbstractAction, but to no effect.

/**
 * @Entity
 * @Table(name="actions")
 */
class BarAction extends AbstractAction
{
   ...
}

Omitting the @Table gives me a PDOException about a missing table BarAction. Using the @Table of the base class gives me:

PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

So, how do I map this?

Edit: So far I have tried two more things.

I tried removing the @Entity as well as the @Table from BarAction in the hope that this way it would no longer require a database table. That doesn't work. Instead, I get this error:

Doctrine\ORM\Mapping\MappingException: Class BarAction is not a valid entity or mapped super class.

Next I tried creating an actions_bar table in my database with just a single foreign key column id. Then I mapped the BarAction to it. That works (yay!) but it feels crufty and ugly to have an extra SQL table that I don't need at all.

So, still looking for a better way...

解决方案

You are using joined inheritance model (class table inheritance), which uses a separate table for parent and each child. If you don't specify any fields in the child class, Doctrine will just create a table only containing ID field.

And a parent class can only use one type of inheritance, either class table inheritance or single table inheritance.

In that case, if you don't want to have a table with only id column in it, you need to change your data model.

这篇关于当一个子类没有额外的属性时,Doctrine表类继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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