douao1858 2013-12-26 19:01
浏览 10
已采纳

使用油CLI在FuelPHP中更改表结构

I created a table using oil generate CLI command and the table was created as required.

php oil generate scaffold <details>

Now I wanted to change few columns and add a new column to the table strcutre. I used the oil refine and it ran.

php oil refine migrate -all

But there is no changes done to the table. Migration details are provided below (chnage of a column - 'created').

Old migration file: 003_create_categories.php

<?php

namespace Fuel\Migrations;

class Create_categories
{
    public function up()
    {
    \DBUtil::create_table('categories', array(
        'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true, 'unsigned' => true),
        'name' => array('constraint' => 255, 'type' => 'varchar'),
        'description' => array('type' => 'text'),
        'image' => array('constraint' => 255, 'type' => 'varchar'),
        'created' => array('type' => 'datetime'),
        '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('categories');
    }
}

New migration file: 004_create_categories.php

<?php

namespace Fuel\Migrations;

class Create_categories
{
    public function up()
    {
    \DBUtil::create_table('categories', array(
        'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true, 'unsigned' => true),
        'name' => array('constraint' => 255, 'type' => 'varchar'),
        'description' => array('type' => 'text'),
        'image' => array('constraint' => 255, 'type' => 'varchar'),
        'status' => array('type' => 'tinyint'),
        '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('categories');
    }
}
  • 写回答

1条回答 默认 最新

  • dsfsdf7852 2013-12-27 15:58
    关注

    Migration files are designed to build on each other. You don't need to create the table in every migration. Fuel will run your 003 migration, and then your 004 migration which will fail because the table already exists.

    Your 004 migration should only contain a description of how to update the table from its previous state. Ie, add your new column.

    The "down" of your migration should do the opposite as well. So your 004 migration's down should remove the column.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?