doupo5861 2012-11-26 19:30
浏览 26

Yii迁移和自定义抽象数据类型

I'm working on a project and using Yii's Migration feature to keep the different production and test systems in sync. I must say i love this tool. My question is there a way to create custom Abstract Data Types?

I know Yii's migration feature is made to allow table creation in multiple DBMS systems but my site is limited to MySQL so that should help things along. What I would like to do is:

$this->createTable('test_table', array(
      'key'=>'pk',
      'active_YN'=>'yn',
));

instead of:

$this->createTable('test_table', array(
      'key'=>'pk',
      'active_YN'=>'TINYINT(1) NOT NULL DEFAULT \'1\'',
));

Im guessing that i would have to extend CDbMigration, possibly with a behavior?

Many Thanks.

  • 写回答

1条回答 默认 最新

  • duanlinzhen7235 2012-11-26 23:18
    关注

    If you still want to make the trick, here is what you can try.

    MySQL uses the driver CMysqlSchema by default. You need to extend this class with your custom abstract column types.

    class MyCustomMysqlSchema extends CMysqlSchema
    {
        /**
         * @var array the abstract column types mapped to physical column types.
         */
        public $columnTypes=array(
            'pk' => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY',
            'string' => 'varchar(255)',
            'text' => 'text',
            'integer' => 'int(11)',
            'float' => 'float',
            'decimal' => 'decimal',
            'datetime' => 'datetime',
            'timestamp' => 'timestamp',
            'time' => 'time',
            'date' => 'date',
            'binary' => 'blob',
            'boolean' => 'tinyint(1)',
            'money' => 'decimal(19,4)',
            // add your custom abstract column types here
            'yn' => 'tinyint(1) UNSIGNED NOT NULL DEFAULT 1',
        );
    }
    

    You need your connection to use this new driver. Modify your db configuration as follow.

    'db'=>array(
        // your initial db configuration here
        'driverMap'=>array('mysql'=>'MyCustomMysqlSchema'),
    ),
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)