doctrine - Doctrine 多对一多目标实体
问题描述
我在我的项目中使用 Doctrine ORM,但我的表有问题:
- 设备(id、名称、ip_address)
- 设备组(ID,名称)
- 模板(id、object_type、object_id)
因此,这三个实体从一个超类扩展为常见字段,例如 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 脚本)。
你有什么主意吗 ?
提前谢谢你,
解决方案
推荐阅读
- python - 是否有一个 API 可以根据商品的 EAN 代码查询 Amazon.de(德国亚马逊)商品的属性(卖家排名和价格)?请务必是 Python 代码
- perl - 如何使用 Perl 将矩阵(成对比较)转换为列?
- python - python & DAL & SQLite - 尝试跨 python 会话使用持久数据
- django-models - 如何在自我表示中显示 Django 模型字段的前 x 个字符
- python - 使用 Vscode 调试 Python 给出指向 @@ 符号的语法错误
- javascript - 在 Postman 中解析 JSON 响应数组中的值
- python - 如何用正则表达式替换字符串列表中的字符串?
- vba - VBA 公共变量不起作用 - MS Word - 计算表中的字符
- python - 循环中的绘图挂起matplotlib:奇怪的内存泄漏?
- php - Docker+php-fpm+Xdebug (Linux) + PhpStorm (Windows)