原则继承映射,按类型和子类型参数选择


Doctrine Inheritance mapping, selecting by type and subtype parameter

我有一个超类LibraryCommon和4个子类型:Library, PurchasedLibrary, PurchasableLibraryPersonalLibrary

LibraryLibraryCommon的简单扩展,没有额外的字段,PurchasedLibrary有一个用户实体。在一个实例中,我想要属于某个用户的所有Libraries和所有PurchasedLibraries

所以我在LibraryCommonRepository中创建了一个方法:
public function findLibraries($user)
{
    return $this->createQueryBuilder('l')
        ->where('l INSTANCE OF Library')
        ->orWhere('l INSTANCE OF PurchasedLibrary AND l.user = :user')
        ->setParameter(':user', $user)
        ->getQuery()
        ->getResult()
    ;
}

但是,这个错误显示为[Semantical Error] line 0, col 182 near 'user = :user': Error: Class LibraryCommon has no field or association named user

我错过了什么,或者我真的需要连接两个单独的查询来获得我想要的结果吗?


另外,如果我不提供用户并且这样做:

public function findLibraries()
{
    return $this->createQueryBuilder('l')
        ->where('l INSTANCE OF Library OR l INSTANCE OF PurchasedLibrary')
        ->getQuery()
        ->getResult()
    ;
}
生成的查询如下所示:
SELECT 
...
FROM 
  library l0_ 
WHERE 
  (
    l0_.type IN ('library') 
    OR l0_.type IN ('purchased')
  ) 
  AND l0_.type IN (
    'library', 'personal', 'purchased', 
    'purchasable'
  )

是否有办法使查询只做WHERE l0_.type IN ('library', 'purchased')

您可能想要将User映射添加到您的LibraryCommon,只是私有属性,默认为null,并在PurchasedLibrary中保留getter和setter