0

So, i have a field in my database is_good, type boolean. So if i check in my database, i can see TRUE or FALSE in my datas.

I have a simple query with Doctrine and i simply check if my_table.is_good = $isgood.

I have var_dump($isgood) and i have a boolean in this variable.

When i execute my query, i have this error (in french) :

SQLSTATE[42883]: Undefined function: 7 ERREUR:  l'opérateur n'existe pas : boolean = integer

And the query in the error message show me that : my_table.is_good = 1

I don't undestand why i can't compare field = boolean ?

Edit :

This is my first var_dump($isgood) :

<pre class='xdebug-var-dump' dir='ltr'><small>boolean</small> <font color='#75507b'>true</font></pre>

I have try to put 'true' and 'false' in my variable, so var_dump($isgood) show me a string, and it's ok for the query.. I don't understand !

3
  • Please show us your exact var_dump($isgood) results. Commented May 26, 2014 at 15:45
  • I edit my post, if i put a string 'true' or 'false' in my variable it's ok... Commented May 26, 2014 at 15:47
  • Am I understand right, you set up your query this way: ...->createNativeQuery("SELECT ... WHERE my_table.is_good = $isgood")? Commented May 26, 2014 at 15:59

1 Answer 1

3

You should use prepared statements, if you want to insert variables inside your query. In doctrine (with native queries) you can use the NativeQuery's setParameter function:

<?php
use Doctrine\ORM\Query\ResultSetMapping;

$rsm = new ResultSetMapping();
// build rsm here

$sql = 'SELECT something FROM my_table WHERE my_table.is_good = ?';
$query = $entityManager->createNativeQuery($sql, $rsm);
$query->setParameter(1, $isgood);
$somethings = $query->getResult();

There is also a third parameter to set the parameter's type explicitly, but boolean is detected automatically.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.