将一组实例类型绑定为Doctrine DQL的参数

时间:2017-06-28 00:52:14

标签: php doctrine dql

是否可以将类型数组绑定到DQL查询以与INSTANCE OF一起使用?

例如,这是用于通过单个指定类型查询实体的Doctrine DQL。

<?php
$dql = <<<DQL
SELECT o FROM \MyProj\AbstractMyObject AS o 
WHERE o INSTANCE OF :type
DQL;
$q = $em->createQuery($dql);
$q->setParameter(
    'type',
    $em->getClassMetadata(AlphaObject::class);
);
$q->getResult();

将多个类型作为参数绑定到此查询的等效方法是什么?

这就是我的尝试。它没有错误,但它也不会返回任何结果。

<?php
$dql = <<<DQL
SELECT o FROM \MyProj\AbstractMyObject AS o 
WHERE o INSTANCE OF (:types)
DQL;
$q = $em->createQuery($dql);
$q->setParameter(
    'types',
    [
        $em->getClassMetadata(AlphaObject::class),
        $em->getClassMetadata(BetaObject::class)
    ]
);
$q->getResult();

1 个答案:

答案 0 :(得分:1)

试试这个:

<?php
$dql = <<<DQL
SELECT o FROM \MyProj\AbstractMyObject AS o 
WHERE o INSTANCE OF :typeAlpha OR o  INSTANCE OF :typeBeta
DQL;
$q = $em->createQuery($dql);
$q->setParameters([
    'typeAlpha' => $em->getClassMetadata(AlphaObject::class),
     'typeAlpha' => $em->getClassMetadata(BetaObject::class)
    ]
);
$q->getResult();