相关疑难解决方法(0)

将鉴别器列映射到具有Doctrine 2的字段

在我的项目中,我有几个类表继承如下:

namespace MyProject\Model;

/**
 * @Entity
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="discr", type="string")
 * @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
 */
class Person
{
    // ...
}

/** @Entity */
class Employee extends Person
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

我有一个方法,它根据具有公共getter的字段将实体转换为数组.这里的问题是我丢失了数组中的继承信息,因为鉴别器值没有存储在字段中.

所以我尝试的是以下内容,希望教义会自动设置$disc:

class Person
{
    // can I automatically populate this field with 'person' or 'employee'?
    protected $discr;

    public function getDiscr() { return $this->discr; }
    public function setDiscr($disc) { $this->discr; }

    // ...
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在学说中使这项工作?或者我需要在实体到数组方法中读取类元数据?

doctrine-orm

9
推荐指数
1
解决办法
2万
查看次数

标签 统计

doctrine-orm ×1