php - Doctrine 2 : Is there a way to inherit mapping from a trait using yaml or xml?

标签 php symfony doctrine-orm doctrine

我找到了 following example in the doctrine documentation他们在特征中添加了映射:

/**
 * Trait class
 */
trait ExampleTrait
{
    /** @Id @Column(type="string") */
    private $id;

    /**
     * @Column(name="trait_foo", type="integer", length=100, nullable=true, unique=true)
     */
    protected $foo;

    /**
     * @OneToOne(targetEntity="Bar", cascade={"persist", "merge"})
     * @JoinColumn(name="example_trait_bar_id", referencedColumnName="id")
     */
    protected $bar;
}

我正在尝试映射特征,而不必在继承它的类中复制映射。我没有诚实地在上面尝试过这个,因为我当前的项目正在使用 yaml 进行映射,但它看起来像一个普通的 php 类在使用特征时也会继承映射。

有没有一种方法可以不使用关联而是使用 yaml 或 xml 来继承此特征的映射?我尝试将特征设置为 mapped superclass但它没有用,但我基本上正在寻找相同类型的想法。

谢谢。

最佳答案

使用 YAML 声明 mappedSupperClass:

Namespace\For\Your\MappingClass:
    type: mappedSuperclass
    fields:
        id:
            id:
                type: integer
                generator:
                    strategy: AUTO

        ... other fields and relations 

用 XML 声明它:

<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                  http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <mapped-superclass name="Namespace\For\Your\MappingClass">

        <field name="foo" column="foo" type="string" length="255" />

        <field name="bar" column="bar" type="string" length="255" unique="true" />

        ... other fields

    </mapped-superclass>

</doctrine-mapping>

如果您运行 app/console doctrine:generate:entities,您将能够在其他实体中使用 mappedSuperClass 作为 ascendant。

关于php - Doctrine 2 : Is there a way to inherit mapping from a trait using yaml or xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26366071/

相关文章:

php - ajax 动态对象(类似关联数组)通过 PDO 转换为两个 MySQL 表

php - 在变量中存储 PHP 语法

php - 如何在 PHP Nusoap 中解决 "XML error parsing SOAP payload on line 1: Invalid document end"

php - Doctrine2 - 列在刷新之前更改为空

javascript - 根据表单输入更改 html 内容

php - Symfony2 datetime 存储时间戳的最佳方式?

Symfony2 DateTime null 接受

mongodb - 如何在 symfony2 中将 mongo 文档与子文档映射

php - 教义查询生成器 : having/where error using alias

php - Symfony2 给出空白页