我们可以使用traits来映射manyToOne与doctrine2的关系吗? [英] can we use traits to map manyToOne relationship with doctrine2?

查看:148
本文介绍了我们可以使用traits来映射manyToOne与doctrine2的关系吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常将其他人的投票实体与symfony2 / doctrine2相关联。这是通过manyToOne关系完成的。



我正在考虑使用特征来包含该协会和其他实体的getter / setter,但是后来我遇到了无法正确替换mappedBy参数的问题。



如果没有办法给出参数一个特质,我还能如何实现我的目标,知道我不能扩展另一个班级。



示例:

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

以前的映射适用于一个邮政实体。但是如果我把它放在一个特征中并在注释实体中使用trait,那么我怎么可以将mappedBy属性从post更改为comment?



非常感谢! p>

解决方案

是的,您可以覆盖与

  * @AssociationOverrides({
* @AssociationOverride(name =bar,
* joinColumns = @ JoinColumn(
* name =example_entity_overridden_​​bar_id,referencedColumnName =id
*)
*)
*})

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


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!

解决方案

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

这篇关于我们可以使用traits来映射manyToOne与doctrine2的关系吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆