Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine doesn't persist entity with boolean values and PDO::ATTR_EMULATE_PREPARES = false in Mysql

We are using Symfony to create some webservices. We use Doctrine-ORM to store entities and Doctrine-DBAL to retrive data because it's very light and can reuse the ORM (entity manager) connection.

When using Doctrine-DBAL, integer values are returned to PHP as strings and we want to have integer values, specially because they are retured to Javascript. Following this discussion How to get numeric types from MySQL using PDO? we have installed mysql native driver sudo apt-get install php5-mysqlnd and setup our symfony (dbal) configuration with PDO::ATTR_EMULATE_PREPARE = false :

doctrine:
    dbal:
         .
         .

         options:
            20 : false # PDO::ATTR_EMULATE_PREPARES is 20

With this configuration we are getting integers when mysql fields are integers. So far so good.

But there is a new problem: When storing entities with boolean values through Doctrine-ORM the entity is not persisted. We see in the logs the INSERT and the COMMIT, but the record is not in the database (if we use a table with no boolean fields defined in the entity, the record is stored).

Furthermore, we don't get any Error or Exception, so we find this very dangerous. We think there is a bug in the PDO library but we have to look a bit more into it.

The question: Has anybody experienced this behaviour? any workaround? Should Doctrine account for this?

like image 497
Dmt Avatar asked Jun 27 '15 14:06

Dmt


2 Answers

gseric's answer will work but with the effect of hydrating your entities with integers. To still get booleans in your entities you can simply extend Doctrine's BooleanType:

class BooleanToIntType extends \Doctrine\DBAL\Types\BooleanType
{
    public function getBindingType()
    {
        return \PDO::PARAM_INT;
    }
}

Then, in your application bootstrap:

\Doctrine\DBAL\Types\Type::overrideType('boolean', BooleanToIntType::class);
like image 156
webbiedave Avatar answered Nov 15 '22 17:11

webbiedave


If it's not too late for you, you can fix this issue in your app bootstrap this way:

\Doctrine\DBAL\Types\Type::overrideType('boolean', 'Doctrine\\DBAL\\Types\\IntegerType');

After this line is executed Doctrine DBAL will map your PHP boolean values to PDO integers (PDO::PARAM_INT instead od PDO::PARAM_BOOL).

like image 28
gseric Avatar answered Nov 15 '22 19:11

gseric