Doctrine does not save object with boolean values โ€‹โ€‹and PDO :: ATTR_EMULATE_PREPARES = false in Mysql

We use Symfony to create some web services. We use Doctrine-ORM to store objects and Doctrine-DBAL to retrieve data because they are very lightweight and can reuse the ORM (entity manager) connection.

With Doctrine-DBAL, integer values โ€‹โ€‹are returned in PHP as strings, and we want integer values, especially because they are stored in Javascript. After this discussion, How to get numeric types from MySQL using PDO? we installed our own mysql driver sudo apt-get install php5-mysqlnd and configured our symfony (dbal) configuration with PDO :: ATTR_EMULATE_PREPARE = โ€‹โ€‹false:

 doctrine: dbal: . . options: 20 : false # PDO::ATTR_EMULATE_PREPARES is 20 

In this configuration, we get integers when the mysql fields are integers. So far so good.

But a new problem arises: when storing objects with Boolean values โ€‹โ€‹through Doctrine-ORM, the entity is not saved. In the logs, we see INSERT and COMMIT, but the record is not in the database (if we use a table without logical fields defined in essence, the record is saved).

In addition, we do not receive any errors or exceptions, so we believe that this is very dangerous. We believe that there is a mistake in the PDO library, but we need to work on it a bit.

Question: Has anyone experienced this behavior? any workaround? Should this account be accounted for?

+6
source share
2 answers

gseric answer will work, but with the effect of moisturizing your entities with integers. To continue to receive boolean in their essence, you can simply extend the Doctrine BooleanType :

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

Then in your bootstrap application:

 \Doctrine\DBAL\Types\Type::overrideType('boolean', BooleanToIntType::class); 
+5
source

If you are not too late, you can fix this problem in your application bootstrap:

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

After executing this line, Doctrine DBAL maps your PHP booleans to PDO integers (PDO :: PARAM_INT instead of od PDO :: PARAM_BOOL).

+3
source

Source: https://habr.com/ru/post/989857/


All Articles