我找到了 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/