【发布时间】:2013-06-17 02:40:05
【问题描述】:
我正在尝试从我的模型中找到设计实体之间关系的最佳方式。我会尽量解释清楚。
想象以下 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;
}
我想建立两个一对多的双向学说关系,其中Cat 和Dog 是关系的拥有方。 Cat 和 Dog 类都有这个实体配置:
manyToOne:
imageHistory:
targetEntity: ImageHistory
joinColumn:
name: image_history_id
referencedColumnName: id
如何表示te关系的另一边?
oneToMany:
owner:
targetEntity: <What can I write here?>
mappedBy: imageHistory
我想象一个解决方案,其中Cat 和Dog 继承Animal 实体类,因此我可以将ManyToOne 关系移动到Animal 类并将Animal 作为OneToMany 关系的targetEntity。但是如果我有一个新的SoundHistory 实体并且:Cat、Dog 和新的Car 和Boat 类必须与它有关系,问题就会再次出现。
A 不能只是将 SoundHistory 添加为与 Animal 类的 oneToMany 关系,因为 Car 和 Boat 不会从它继承。所以我仍然无法在ImageHistory 实体中填充我的OneToMany 关系的targetEntity。
在这种情况下设计实体模型的最佳方法是什么?
【问题讨论】:
-
您能解释一下 SQL 中的关系吗?
-
使用一些 AbstractEntity?有点 MotherOfAllObjects/Entities
-
你可以使用@OneToMany注解。见enter link description here
标签: php oop doctrine-orm