设计实体模型来管理多个双向关系

map*_*phe 12 php oop doctrine-orm

我正试图找到设计模型中实体之间关系的最佳方法.我会试着清楚地解释一下.

想象一下以下的Doctrine2实体:

class ImageHistory
{
    /**
     * @var Image
     */
    protected $current;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    protected $old;
}

class Dog
{
    protected $name;

    /**
     * @var ImageHistory
     */
    protected $imageHistory;
}

class Cat
{
    protected $name;

    /**
     * @var ImageHistory
     */
    protected $imageHistory;
}
Run Code Online (Sandbox Code Playgroud)

我想建立两个一对多的双向学说关系,Cat并且Dog是关系的拥有方.两个CatDog类都有这个实体配置:

manyToOne:
    imageHistory:
        targetEntity: ImageHistory
        joinColumn:
            name: image_history_id
            referencedColumnName: id
Run Code Online (Sandbox Code Playgroud)

如何表示te关系的另一面?

oneToMany:
    owner:
        targetEntity: <What can I write here?>
        mappedBy: imageHistory
Run Code Online (Sandbox Code Playgroud)

我想象一个解决方案,其中CatDog继承一个Animal实体类,所以我可以将ManyToOne关系移动到Animal类中,并将其Animal作为OneToMany关系的targetEntity.但是,如果我有一个新的问题再次出现SoundHistory:实体Cat,Dog以及新的CarBoat类都必须有一个相对于它.

A不能只是添加SoundHistory的一对多关系到Animal类,因为CarBoat不会从中inherite.所以我仍然无法targetEntityImageHistory实体中填充我的OneToMany关系.

在这种情况下,设计实体模型的最佳方法是什么?

Leo*_*eon 0

多对一关系是单向的,因此您无法代表另一方。

另外,如果您确实想将狗和猫存储在同一个表中,则应该考虑创建一个超级实体。

  • 实际上,多对一可以是 bidir (参见 https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#one-to-many-bidirection) 。 (2认同)