Doctrine Trait覆盖OneToMany关系

时间:2017-03-18 15:05:45

标签: php symfony doctrine-orm doctrine

我有trait,看起来像这样

trait CommentableTrait
{
    /**
     * @OneToMany(targetEntity="Comment", mappeBy="to_override")
     *
     */
    protected $comments;
}

Comment.php我有ManyToOne关系

class Comment
{
    /**
     * @ManyToOne(targetEntity="Article", inversedBy="comments")
     */
    protected $article;

    /**
     * @ManyToOne(targetEntity="Page", inversedBy="comments")
     */
    protected $page;

     /**
     * @ManyToOne(targetEntity="Page", inversedBy="comments")
     */
    protected $question;

} 

我想在我的实体Article.phpPage.phpQuesion.php中使用此特征,如下所示:

class Article
{
    use CommentableTrait;
}

我需要覆盖mappedBy中的CommenatableTrait

我已尝试过Doctrine - Override Field Association Mappings In Subclasses

的解决方案

它适用于OneToOneManyToMany关系,但我无法找到双向OneToMany - ManyToOne关系的工作示例。有办法吗?

1 个答案:

答案 0 :(得分:1)

您可以在一个类中使用trait并覆盖那里的关系。例如:

class Article
{
    use CommentableTrait;

    /**
     * @See CommentableTrait
     * @OneToMany(targetEntity="Comment", mappeBy="article")
     *
     */
     $comments      
}