I have to do some queries, while i was trying different ways, i found out that the next lines are not "recognized" by doctrine (it gives errors):

for example, when i want to compare if some data in the db is equal to a literal, here the condition:

('u.gender = M')

this is how my table look like:

id  gender
 1     M
 2     M
 3     F

it throws a semantical error. Also when comparing dates that way.

I would like to know why this is not recognized by doctrine, while comparing directly with numbers is accepted:

condition: ('u.age = 15')

有帮助吗?

解决方案

First option you can do this way-

 $M = 'M';
 $age = 15;
 $query = $this->createQueryBuilder('t')
                ->where('u.gender = :M AND u.age = :age')
                ->setParameters(array('M'=> $M,'age'=>$age);

another way to do this-

$query = $this->createQueryBuilder("t")
              ->where("u.gender = 'M' AND u.age = 15");

其他提示

So i guess the answer to my question would be that it was not working because doctrine didn't recognize M as a string. That is why it was necessary to use inverted commas like @Mehedi said.

Another way of solving this was to use the query builder:

    $v = 'M';
    $condition = $this->qb->expr()->eq('u.gender', $this->qb->expr()->literal($v));

but i guess that is just long and hard to read. So the shortest thing would be just:

    $condition = ("u.gender = 'M'");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top