原则 2 - 如何在 where 子句中使用鉴别器列

发布于 2024-11-06 23:26:34 字数 328 浏览 0 评论 0原文

我在这样的 where 子句中使用了鉴别器列:

//f = root entity
$qb = $this->createQueryBuilder('f');
$qb->add('where', 'f.format = \'image\' OR f.format = \'text\'');

我收到错误:“消息:[语义错误]第 0 行,第 73 列靠近 'format = 'image'':错误:类 Entities\File\AbstractFile 没有字段或关联命名格式”

如何在 where 子句中使用鉴别器列?

谢谢。

I was used discriminator column in where clause like this:

//f = root entity
$qb = $this->createQueryBuilder('f');
$qb->add('where', 'f.format = \'image\' OR f.format = \'text\'');

I've got an error: "Message: [Semantical Error] line 0, col 73 near 'format = 'image'': Error: Class Entities\File\AbstractFile has no field or association named format"

How can i use discriminator column in where clause?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

四叶草在未来唯美盛开 2024-11-13 23:26:34

我认为你应该使用 实例

I think that you should use INSTANCE OF

对你的占有欲 2024-11-13 23:26:34

它在查询生成器中看起来像这样:

$class = 'Entity\File\Image';

$qb = $this->createQueryBuilder('f');
$qb->where($qb->expr()->isInstanceOf('f', $class));

注意:您将无法将类设置为参数,因为它将被转义。

It would look in query builder like this:

$class = 'Entity\File\Image';

$qb = $this->createQueryBuilder('f');
$qb->where($qb->expr()->isInstanceOf('f', $class));

Note: that you will not be able to set the class as a parameter because it will be escaped.

梦归所梦 2024-11-13 23:26:34

对于 PHP 5.50 及以上版本:

$this->createQueryBuilder('f')
        ->andWhere('f INSTANCE OF '.Image::class)

for PHP 5.50 and above:

$this->createQueryBuilder('f')
        ->andWhere('f INSTANCE OF '.Image::class)
九八野马 2024-11-13 23:26:34

作为最新的学说版本,支持直接查询鉴别器值。

public function findOfType($discr)
    {
        $qb = $this->createQueryBuilder('e');
        $qb->where('e INSTANCE OF :discr');
        $qb->setParameter('discr', $discr);
        return $qb->getQuery()->getResult();
    }

将有一个带有此子句的结果查询:

WHERE e0_.discr IN ('discriminator_passed_to_function')

As this latest doctrine version it is supported to query directly the discriminator value.

public function findOfType($discr)
    {
        $qb = $this->createQueryBuilder('e');
        $qb->where('e INSTANCE OF :discr');
        $qb->setParameter('discr', $discr);
        return $qb->getQuery()->getResult();
    }

will have a result query with this clause:

WHERE e0_.discr IN ('discriminator_passed_to_function')
魂牵梦绕锁你心扉 2024-11-13 23:26:34

这个学说扩展对我来说非常有用,因为我需要访问父类,而 INSTANCE OF 在这种情况下不起作用。

https://gist.github.com/jasonhofer/8420677

例如:我有以下内容类结构:

BaseClass

Class1 继承自 BaseClass (discriminator = c1)

Class2 继承来自 Class1 (discriminator = c2)

Class3 继承自 Class1 (discriminator = c3)

我想选择 Class1 中的所有实体strong> 但不是来自 Class2Class3

SELECT c FROM \Class1 c WHERE TYPE(c) = 'c1';

This doctrine extension was very useful for me because I needed to access the parent class and INSTANCE OF doesn't works in that case.

https://gist.github.com/jasonhofer/8420677

For example: I have the following class structure:

BaseClass

Class1 inherits from BaseClass (discriminator = c1)

Class2 inherits from Class1 (discriminator = c2)

Class3 inherits from Class1 (discriminator = c3)

I want to select all entities from Class1 but not from Class2 or Class3

SELECT c FROM \Class1 c WHERE TYPE(c) = 'c1';
望笑 2024-11-13 23:26:34

您所需要的一切:

return $this->createQueryBuilder('entity')
            ->andWhere('entity.field = :field')
            ->leftJoin('entity.entity2', 'entity2')
            ->andWhere('entity2 INSTANCE OF :type')
            ->setParameter('field', 'value')
            ->setParameter('type', $this->getEntityManager()->getClassMetadata(Entity2::class))
            ->getQuery()
            ->getResult()
            ;

All you need:

return $this->createQueryBuilder('entity')
            ->andWhere('entity.field = :field')
            ->leftJoin('entity.entity2', 'entity2')
            ->andWhere('entity2 INSTANCE OF :type')
            ->setParameter('field', 'value')
            ->setParameter('type', $this->getEntityManager()->getClassMetadata(Entity2::class))
            ->getQuery()
            ->getResult()
            ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文