Doctrine2 workaround for mapping MySql 'bit' data type

In case you are using BIT column to store a boolean, you do this:

// get currently used platform
$dbPlatform = $em->getConnection()->getDatabasePlatform();

// interpret BIT as boolean
$dbPlatform->registerDoctrineTypeMapping('bit', 'boolean');

Now every time you map a property to bit column, doctrine 2 will interpret its value as a boolean.


You could create your own, custom type for Doctrine.

  1. Create a new type by extending Doctrine\DBAL\Types\Type class.
  2. Override convertToPHPValue() and convertToDatabaseValue() methods.
  3. Register a new type:

    \Doctrine\DBAL\Types\Type::addType('abc', 'Your\\Custom\\Type\\AbcType');
    
    $dbPlatform = $em->getConnection()->getDatabasePlatform();
    $dbPlatform->registerDoctrineTypeMapping('abc', 'abc');
    

Read more on Doctrine's documentation pages


Use mapping_types in the config.yml

doctrine:
    dbal:
        driver:%% database_driver
        host:%% database_host
        port:%% database_port
        dbname:% database_name%
        user:%% database_user
        password:%% database_password
        charset: UTF8
        mapping_types:
            bit: boolean