facebook twitter hatena line google mixi email

Php/fuelphp/migration/model orm

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

migrationを使ってormのmodelを作成する

php oil g model user name:varchar[31] email:string[63] password:string[31] msg:text 

以下2つのファイル(modelとmigration)ができる

  • fuel/app/classes/model/user.php
 class Model_User extends \Orm\Model
 {
     protected static $_properties = array(
         'id',
         'name',
         'email',
         'password',
         'msg',
         'created_at',
         'updated_at',
     );
     protected static $_observers = array(
         'Orm\Observer_CreatedAt' => array(
             'events' => array('before_insert'),
             'mysql_timestamp' => false,
         ),
         'Orm\Observer_UpdatedAt' => array(
             'events' => array('before_update'),
             'mysql_timestamp' => false,
         ),
     );
     protected static $_table_name = 'users';
 }
  • fuel/app/migrations/002_create_users.php
 namespace Fuel\Migrations;
 class Create_users
 {
     public function up()
     {
         \DBUtil::create_table('users', array(
             'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true, 'unsigned' => true),
             'name' => array('constraint' => 31, 'type' => 'varchar'),
             'email' => array('constraint' => 63, 'type' => 'varchar'),
             'password' => array('constraint' => 31, 'type' => 'varchar'),
             'msg' => array('type' => 'text'),
             'created_at' => array('constraint' => 11, 'type' => 'int', 'null' => true),
             'updated_at' => array('constraint' => 11, 'type' => 'int', 'null' => true),
         ), array('id'));
     }
     public function down()
     {
         \DBUtil::drop_table('users');
     }
 }


  • 以下コマンド実行でmigrationが実行されtableができる
php oil refine migrate
  • こんなテーブルができる
CREATE TABLE `users` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `name` varchar(31) NOT NULL,
 `email` varchar(63) NOT NULL,
 `password` varchar(31) NOT NULL,
 `msg` text NOT NULL,
 `created_at` int(11) DEFAULT NULL,
 `updated_at` int(11) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;