Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ChangingTable Structure in FuelPHP with Oil CLI
  • 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');
        }
    }
  • HarroHarro
    Accepted Answer
    You can not call create_table() a second time, it already exists.

    You have to create a migration that uses DBUtil::add_fields() and DBUtil::drop_fields() to modify the structure of the existing table. This has as an added benefit that you don't loose existing data, which you would if you would drop and re-create the entire table.
  • Thanks. I got it.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion