Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine 2 - How to use discriminator column in where clause

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.

like image 512
Can Aydoğan Avatar asked May 13 '11 07:05

Can Aydoğan


3 Answers

I think that you should use INSTANCE OF

like image 165
Koc Avatar answered Oct 18 '22 12:10

Koc


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.

like image 14
Aistis Avatar answered Oct 18 '22 12:10

Aistis


for PHP 5.50 and above:

$this->createQueryBuilder('f')
        ->andWhere('f INSTANCE OF '.Image::class)
like image 4
Andrew Atkinson Avatar answered Oct 18 '22 13:10

Andrew Atkinson