用于映射MySql"位"数据类型的Doctrine2解决方法

Ale*_*rth 16 mysql mapping doctrine-orm

我的数据库模式中有一些列具有位数据类型,并且在Doctrine2映射时出现问题.我一直在:

请求未知的数据库类型位,Doctrine\DBAL\Platforms\MySqlPlatform可能不支持它.

有什么工作吗?我只是想将数据类型更改为boolean并使用true和false语句,但这意味着要大规模地更改模式,而我没有时间.

Sep*_*eph 33

在config.yml中使用mapping_types

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
Run Code Online (Sandbox Code Playgroud)


Art*_*era 11

如果您使用BIT列来存储a boolean,则执行以下操作:

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

// interpret BIT as boolean
$dbPlatform->registerDoctrineTypeMapping('bit', 'boolean');
Run Code Online (Sandbox Code Playgroud)

现在,每次将属性映射到位列时,doctrine 2都会将其值解释为布尔值.


Cro*_*zin 6

您可以为Doctrine创建自己的自定义类型.

  1. 通过扩展Doctrine\DBAL\Types\Type类来创建新类型.
  2. 覆盖convertToPHPValue()convertToDatabaseValue()方法.
  3. 注册新类型:

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

阅读Doctrine的文档页面