如何编写一个DQL select语句来搜索一些表,但不是单个表继承表中的所有实体

bla*_*e24 13 doctrine-orm

所以我在一个表中有3个实体.我需要能够在一个select语句中搜索3个实体中的2个,但我不知道如何做到这一点.

Max*_*Max 18

INSTANCE OF像这样在你的dql查询中使用运算符(User你的基类在哪里):

$em->createQuery('
    SELECT u 
    FROM Entity\User u 
    WHERE (u INSTANCE OF Entity\Manager OR u INSTANCE OF Entity\Customer)
');
Run Code Online (Sandbox Code Playgroud)

Doctrine在WHERE user.type = '...'条件中的sql查询中对此进行转换.

有关dql查询语法的更多详细信息,请参见此处.


小智 5

多个实例的答案实际上不起作用.你必须做这样的事情来检查多个实例.

$classes = ['Entity\Manager', 'Entity\Customer'];
$qb = $this->createQueryBuilder('u');
->where('u.id > 10') //an arbitrary condition, to show it can be combined with multiple instances tests
->andWhere("u INSTANCE OF ('" . implode("','", $classes) . "')");
Run Code Online (Sandbox Code Playgroud)