سؤال

By using cake-DC migration we can write migrations for string,integer can we used ENUM also ???

هل كانت مفيدة؟

المحلول 2

You can only use the data types that the database drivers of the CakePHP ORM support. Enum is not supported, use faked enum instead.

See http://api.cakephp.org/2.4/class-Mysql.html#$columns

array(
    'primary_key' => array('name' => 'NOT NULL AUTO_INCREMENT'),
    'string' => array('name' => 'varchar', 'limit' => '255'),
    'text' => array('name' => 'text'),
    'biginteger' => array('name' => 'bigint', 'limit' => '20'),
    'integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'),
    'float' => array('name' => 'float', 'formatter' => 'floatval'),
    'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
    'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
    'time' => array('name' => 'time', 'format' => 'H:i:s', 'formatter' => 'date'),
    'date' => array('name' => 'date', 'format' => 'Y-m-d', 'formatter' => 'date'),
    'binary' => array('name' => 'blob'),
    'boolean' => array('name' => 'tinyint', 'limit' => '1')
)

You could extend the Mysql source and add that type, but this will break cross-database compatibility of your app. But it is an unlikely case anyway.

نصائح أخرى

I'm just two years late but take a look at this repository. There's a solution for use any data types not supported for CakePHP in migrations.

You'll have a base migration which extends CakeMigration in your application to use instead. It'll make a call for strategies named with the data type, for instance the ENUM type:

https://github.com/leonardolessa/mywallet/blob/master/app/Lib/Migrations/BaseMigration.php

Each strategy implements an interface:

https://github.com/leonardolessa/mywallet/blob/master/app/Lib/Migrations/StrategyInterface.php

The strategy itself just run an ALTER TABLE to add the not supported type in the table:

https://github.com/leonardolessa/mywallet/blob/master/app/Lib/Migrations/EnumStrategy.php

Just don't forget that if you may want use not supported types and cake bake you gonna have a bad time.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top