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

I found the following example in the doctrine's documentation , where they added a match to a tag:

/** * 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; } 

I am trying to render a trait without duplicating the display in classes that inherit it. I have not honestly tried this above, since my current project uses yaml for matching, but it looks like a regular PHP class inherits and renders using a dash.

Is there a way to inherit the mapping for this trait without using associations, but use yaml or xml instead? I tried to set the attribute as a mapped superclass , but it did not work, but I'm basically looking for an idea of ​​the same type.

Thanks.

+6
source share
1 answer

Declare mappedSupperClass using YAML:

 Namespace\For\Your\MappingClass: type: mappedSuperclass fields: id: id: type: integer generator: strategy: AUTO ... other fields and relations 

Declare it using 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> 

If you run app/console doctrine:generate:entities , you can use mappedSuperClass as an upstream in other objects.

0
source

Source: https://habr.com/ru/post/976734/


All Articles