Doctrine doesn't persist entity with boolean v

2020-03-12 03:38发布

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?

2条回答
成全新的幸福
2楼-- · 2020-03-12 04:37

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).

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-03-12 04:42

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);
查看更多
登录 后发表回答