我可以从 php 中的 dotric2 访问鉴别器字段吗? [英] Can I access discriminator field from php in doctrine2?

查看:33
本文介绍了我可以从 php 中的 dotric2 访问鉴别器字段吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样定义继承的实体:

I have an entity which defines inheritance like this:

* @DiscriminatorColumn(name="type", type="string")
* @DiscriminatorMap({"text" = "TextAttribute", "boolean" = "BooleanAttribute", "numeric" = "NumericAttribute", "date" = "DateAttribute"})

我想知道是否可以为字段类型"设置吸气剂?我知道我可以使用 instanceof(在大多数情况下这就是我正在做的)但是很少有 $item->getType() 会让我的生活变得如此轻松的场景.

I am wondering is it possible to have getter for field 'type'? I know I can use instanceof (and in most cases this is what I'm doing) but there are few scenarios where $item->getType() would make my life so much easier.

推荐答案

扩展 beberlei 所说的,你可以在 Attribute 类中声明一些常量,以及一个抽象的 getType() 函数.然后,在每个派生的属性类中重载它.

Extending what beberlei said, you could declare some constants in the Attribute class, and an abstract getType() function. Then, overload it in every derived attribute class.

类似于:

abstract class Attribute {
    const TYPE_BOOL = 0;
    const TYPE_INT  = 1;
    ...
    abstract public function getType();
}

class BooleanAttribute extends Attribute {
    public function getType() {
        return parent::TYPE_BOOL;
    }
}

这篇关于我可以从 php 中的 dotric2 访问鉴别器字段吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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