通过附加表Doctrine 2构建OneToMany和ManyToOne关联超过三个表

时间:2015-07-16 11:54:11

标签: mysql symfony doctrine-orm

建立超过三张桌子的协会 通过附加的[EntityTypes]表Doctrine2

我有下一个数据库模式:

//==== some Tables ====

articles (
    id
    entity_type_id
)

posts (
    id
    entity_type_id
)

other... (
    id
    entity_type_id
)

//==== additional table of Types ====

entity_types (
    id
)

//==== Comments ====

comments (
    id
    entity_type_id
    entity_id
)

我将Symfony2与Doctrine2一起使用,因此我已经编写了下一个协议:

class Article
{
    /**
     * @ORM\ManyToOne(targetEntity="EntityType", inversedBy="articles", cascade={"persist"})
     * @ORM\JoinColumn(name="entity_type_id", referencedColumnName="id")
     */
    protected $entityType;

    /**
     * @ORM\OneToMany(targetEntity="Comment", mappedBy="article")
     */
    protected $comments;
}

class Post
{
    /**
     * @ORM\ManyToOne(targetEntity="EntityType", inversedBy="posts", cascade={"persist"})
     * @ORM\JoinColumn(name="entity_type_id", referencedColumnName="id")
     */
    protected $entityType;

    /**
     * @ORM\OneToMany(targetEntity="Comment", mappedBy="post")
     */
    protected $comments;
}
...

class EntityType
{
    /**
     * @ORM\OneToMany(targetEntity="Article", mappedBy="entityType")
     */
    protected $articles;

    /**
     * @ORM\OneToMany(targetEntity="Post", mappedBy="entityType")
     */
    protected $posts;

    /**
     * @ORM\OneToMany(targetEntity="Comment", mappedBy="entityType")
     */
    protected $comments;
}

class Comment
{
    /**
     * @ORM\ManyToOne(targetEntity="EntityType", inversedBy="comments", cascade={"persist"})
     * @ORM\JoinColumn(name="entity_type_id", referencedColumnName="id")
     */
    protected $entityType;

    /** 
     * @ORM\ManyToOne(targetEntity="Article", inversedBy="comments", cascade={"persist"})
     * @ORM\JoinColumn(name="entity_id", referencedColumnName="id")
     */
    protected $article;

    /** 
     * @ORM\ManyToOne(targetEntity="Post", inversedBy="comments", cascade={"persist"})
     * @ORM\JoinColumn(name="entity_id", referencedColumnName="id")
     */
    protected $post;
}

映射中没有任何错误,并且所有操作都可用 包括评论文章 但是当我试图评论帖子时出现了一个例外

需要有人帮忙......

0 个答案:

没有答案