首页 > 解决方案 > Doctrine 多对一多目标实体

问题描述

我在我的项目中使用 Doctrine ORM,但我的表有问题:

因此,这三个实体从一个超类扩展为常见字段,例如 ID、时间戳......

如果我们看一下模板实体,object_id 字段指的是 device.id 或 device_group.id,具体取决于 object_type 的值(可以是“设备”或“组”的字符串)。如果可能的话,我想知道如何建立具有多个目标的关系。

设备组:

/**
 * @ORM\Entity(repositoryClass="App\Doctrine\Repositories\DeviceGroup")
 * @ORM\Table(name="device_group")
 */
class DeviceGroup extends BaseModel
{

    /**
     * @ORM\Column(type="string", name="name", length=255)
     */
    protected $name;

}

设备

/**
 * @ORM\Entity(repositoryClass="App\Doctrine\Repositories\Device")
 * @ORM\Table(name="device")
 */
class Device extends BaseModel
{

    /**
     * @ORM\Column(type="string", name="name", length=255)
     */
    protected $name;

    /**
     * @ORM\Column(type="string", name="ip_address", length=45)
     */
    protected $ip_address;
}

模板

**
 * @ORM\Entity(repositoryClass="App\Doctrine\Repositories\Template")
 * @ORM\Table(name="template")
 */
class DevicePart extends BaseModel
{
    /**
     * @ORM\Column(type="string", name="object_type", length=6)
     */
    protected $object_type;//can be 'group' or 'device'

    /**
     * Many DevicePart have one Object
     * @ORM\ManyToOne(targetEntity="?????????")
     * @ORM\JoinColumn(name="object_id", referencedColumnName="id")
     */
    protected $objects;

    //target entity can be : App\Doctrine\Entities\Device" or "App\Doctrine\Entities\DeviceGroup

}

我听说过继承映射,并且我已经用我的超类实现了它,我查看了一些示例,但这会让我创建新表,问题是我必须保持当前结构,因为其他程序使用数据(SQL Alchemy 与Python 脚本)。

你有什么主意吗 ?

提前谢谢你,

标签: doctrine

解决方案


推荐阅读