学说2 - 如何在where子句中使用discriminator列

时间:2021-10-28 16:39:22

I was used discriminator column in where clause like this:

我在where子句中使用了discriminator列,如下所示:

//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"

我有一个错误:“消息:[语义错误]第0行,第73行'format ='image'':错误:类实体\文件\ AbstractFile没有字段或关联命名格式”

How can i use discriminator column in where clause?

我怎样才能在where子句中使用discriminator列?

Thanks.

谢谢。

5 个解决方案

#1


51  

I think that you should use INSTANCE OF

我认为你应该使用INSTANCE OF

#2


8  

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.

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

#3


2  

for PHP 5.50 and above:

对于PHP 5.50及以上版本:

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

#4


0  

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')

#5


0  

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.

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

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

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

For example: I have the following class structure:

例如:我有以下类结构:

BaseClass

BaseClass的

Class1 inherits from BaseClass (discriminator = c1)

Class1继承自BaseClass(discriminator = c1)

Class2 inherits from Class1 (discriminator = c2)

Class2继承自Class1(discriminator = c2)

Class3 inherits from Class1 (discriminator = c3)

Class3继承自Class1(discriminator = c3)

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

我想从Class1中选择所有实体,但不从Class2或Class3中选择

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

#1


51  

I think that you should use INSTANCE OF

我认为你应该使用INSTANCE OF

#2


8  

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.

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

#3


2  

for PHP 5.50 and above:

对于PHP 5.50及以上版本:

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

#4


0  

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')

#5


0  

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.

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

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

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

For example: I have the following class structure:

例如:我有以下类结构:

BaseClass

BaseClass的

Class1 inherits from BaseClass (discriminator = c1)

Class1继承自BaseClass(discriminator = c1)

Class2 inherits from Class1 (discriminator = c2)

Class2继承自Class1(discriminator = c2)

Class3 inherits from Class1 (discriminator = c3)

Class3继承自Class1(discriminator = c3)

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

我想从Class1中选择所有实体,但不从Class2或Class3中选择

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