Doctrine2 workaround for mapping MySql 'bit' data type

I have a few columns in my database schema that have bit data types and am having problems with Doctrine2 mapping it. I keep getting:

Unknown database type bit requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it.

Is there any work around? I was thinking of just changing the data type to boolean and just use true and false statements but that would mean changing the schema on a large scale which I dont have time for.

Answers


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

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.


Need Your Help

How can I use a local SMTP server when developing on Windows 7?

iis-7 windows-7 smtp

How can I get SMTP to work on a Windows 7 development box? I used to just be able to turn on the IIS SMTP server on Windows XP. Is SMTP not included with Windows 7? If so, what can I use instead...

git selective revert local changes from a file

git version-control git-svn

In my git repo which is tracking a svn repo I have made a number of edits to a single file.