Doctrine 带信条连接表中的鉴别器
我有许多不相关的实体,我希望能够将FileAttachment实体添加到其中。我正在使用Doctrine2(在Symfony项目的上下文中) 在我开始使用条令之前,我会制作一个带有鉴别器列的连接表,如下所示:Doctrine 带信条连接表中的鉴别器,doctrine,junction-table,discriminator,Doctrine,Junction Table,Discriminator,我有许多不相关的实体,我希望能够将FileAttachment实体添加到其中。我正在使用Doctrine2(在Symfony项目的上下文中) 在我开始使用条令之前,我会制作一个带有鉴别器列的连接表,如下所示: file_id entity_id entity_type 据我所知,对于具有FileAttachment关联的每个实体类型,我都需要一个连接表。如果可能的话,我宁愿避免这样。我找到了一种NHibernate溶液。有没有可能对教义做类似的事情,有没有人能告诉我一些文件?我已经读了(很多次
file_id
entity_id
entity_type
据我所知,对于具有FileAttachment关联的每个实体类型,我都需要一个连接表。如果可能的话,我宁愿避免这样。我找到了一种NHibernate溶液。有没有可能对教义做类似的事情,有没有人能告诉我一些文件?我已经读了(很多次了!)教条手册的章节和内容。但我找不到我想要的 尝试创建一个抽象的
FileAttachment
类,并为每个实体类型扩展它,即EntityOneAttachment
,EntityTwoAttachment
,等等
扩展类都将引用相同的联接列entity\u id
,但将其映射到各自的实体
/**
* @ORM\Entity
* @ORM\Table(name="file_attachment")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="entity_type", type="string")
* @ORM\DiscriminatorMap({"entity_one_attachment" = "EntityOneAttachment", "entity_two" = "EntityTwoAttachment"})
*/
abstract class FileAttachment
{
/**
* @ORM\ManyToOne(targetEntity="File")
* @ORM\JoinColumn(name="file_id", referencedColumnName="id", nullable=false)
**/
protected $file;
}
/**
* @ORM\Entity
*/
class EntityOneAttachment extends FileAttachment
{
/**
* @ORM\ManyToOne(targetEntity="EntityOne", inversedBy="fileAttachments")
* @ORM\JoinColumn(name="entity_id", referencedColumnName="id", nullable=false)
**/
protected $entityOne;
}
/**
* @ORM\Entity
*/
class EntityTwoAttachment extends FileAttachment
{
/**
* @ORM\ManyToOne(targetEntity="EntityTwo", inversedBy="fileAttachments")
* @ORM\JoinColumn(name="entity_id", referencedColumnName="id", nullable=false)
**/
protected $entityTwo;
}
然后将每个实体映射到其各自的附件类
class EntityOne
{
/**
* @ORM\OneToMany(targetEntity="EntityOneAttachment", mappedBy="entityOne")
**/
protected $fileAttachments;
}
这会将附件限制为仅附加到1个实体,对吗?@dnagirl在本例中,每个附件对象恰好映射到1个文件对象和1个实体对象(将其视为文件和实体之间的链接),但不同的附件对象可以链接到同一个文件或实体对象。