2017-06-29 22 views
0

我有两个模型:NoteItem和TaskItem。两者都扩展AbstractStreamItem。Doctrine获取扩展给定抽象类的所有模型的存储库

class AbstractCommentsItem { 
... 
} 
class CommentItem extends AbstractCommentsItem { 
... 
} 
class TaskItem extends AbstractCommentsItem { 
... 
} 

现在我想要在一个getRepository命令中获取两个模型的存储库。

有没有可能?我想过单表继承,但不知道是否是正确的方法。

回答

2

是的,单表继承是一个有效的解决方案。您将可以:

$entityManager->getRepository(AbstractCommentsItem::class); 

为所有子类型获取单个存储库。

我实际上是在我目前的项目中这样做。

+0

好吧,我现在有点困惑。所以AbstractCommentsItem应该是@InheritanceType(“SINGLE_TABLE”)?是否有使用@MappedSuperclass的解决方案,因为呈现的类结构让我更加想起这种方法。 – julew

+1

好吧,@MappedSuperclass不会在这里工作。在文档中发现:“映射超类不能是一个实体,它不能查询”。 – julew