can we use traits to map manyToOne relationship with doctrine2?

喜夏-厌秋 提交于 2019-12-11 18:00:58

问题


I am frequently associating a vote entity in other ones with symfony2 / doctrine 2. This is done through a manyToOne relationship.

I was considering using a trait to include the association and its getters/setters in other entities but then I faced the issue that the mappedBy parameter couldn't be replaced correctly.

If there is no way to give arguments to a trait, how else could I achieve my objective, knowing I can't extend another class.

Example :

/**
 * @ORM\OneToMany(targetEntity="\AppBundle\Entity\Social\Vote", mappedBy="post")
 */
private $votes;

The previous mapping works fine for a post entity. but if I put it in a trait and use the trait in a Comment entity, how can I change the mappedBy attribute from post to comment ?

Thanks a lot!


回答1:


Yes you can override associations with

 * @AssociationOverrides({
 *      @AssociationOverride(name="bar",
 *          joinColumns=@JoinColumn(
 *              name="example_entity_overridden_bar_id", referencedColumnName="id"
 *          )
 *      )
 * })

Have a look at http://doctrine-orm.readthedocs.org/en/latest/tutorials/override-field-association-mappings-in-subclasses.html



来源:https://stackoverflow.com/questions/28564387/can-we-use-traits-to-map-manytoone-relationship-with-doctrine2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!