A complete platform for building web applications.

Migration

Migrations allow your application to create or alter your database schema, file name must be in YYYYMMDDHHIISS + application name + .php format.

Command:

php creator make migration todo

20160907082206_todo.php Created

The file:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_Todo extends ER_Migration {

    /**
     * Class constructor
     *
     * @return  void
     */
    function __construct(){
        parent::__construct();
    }

    /**
     * prepare the migration array
     *
     * @return  void
     */
    public function setup()
    {
        // migration array
        // $this->migration[''] = [];
    }

    // migrating
    public function up()
    {
        // prepare migration array
        $this->setup();
        // migrating the above array
        $this->migrating();
    }

    // un-migrating
    public function down()
    {
        // prepare migration array
        $this->setup();
        // remove migration
        foreach ($this->migration as $table => $fields)
        {
            if(get_instance()->db->count_all($table) == 0)
            {
                get_instance()->dbforge->drop_table($table, TRUE);
            }
        }
    }
}
?>

Now we will edit the $this->migration array to create new tables todo_categories and todo_tasks.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_Todo extends ER_Migration {

    /**
     * Class constructor
     *
     * @return  void
     */
    function __construct(){
        parent::__construct();
    }

    /**
     * prepare the migration array
     *
     * @return  void
     */
    public function setup()
    {
        // migration array
        $this->migration['todo_categories'] = array(
            'name'  =>  'todo_categories',
            'key'   =>  'id',
            'field' =>  array(
                'id' => array(
                    'type' => 'INT',
                    'constraint' => 11,
                    'unsigned' => TRUE,
                    'auto_increment' => TRUE
                ),
               'name' => array(
                    'type' => 'VARCHAR',
                    'constraint' => '100',
                ),
                //table suffix
                'create_by' => array(
                    'type' => 'INT',
                    'constraint' => 11,
                ),
                'update_by' => array(
                    'type' => 'INT',
                    'constraint' => 11,
                ),
                'create_at' => array(
                    'type' => 'datetime',
                ),
                'update_at' => array(
                    'type' => 'datetime',
                ),
                'status' => array(
                    'type' => 'INT',
                    'constraint' => 2,
                ),
            ),
            'attributes' => array('ENGINE' => 'MyISAM'),
        );
        $this->migration['todo_tasks'] = array(
            'name'  =>  'todo_tasks',
            'key'   =>  'id',
            'field' =>  array(
                'id' => array(
                    'type' => 'INT',
                    'constraint' => 11,
                    'unsigned' => TRUE,
                    'auto_increment' => TRUE
                ),
                'category_id' => array(
                    'type' => 'INT',
                    'constraint' => 11,
                ),
               'task' => array(
                    'type' => 'VARCHAR',
                    'constraint' => '100',
                ),
                //table suffix
                'create_by' => array(
                    'type' => 'INT',
                    'constraint' => 11,
                ),
                'update_by' => array(
                    'type' => 'INT',
                    'constraint' => 11,
                ),
                'create_at' => array(
                    'type' => 'datetime',
                ),
                'update_at' => array(
                    'type' => 'datetime',
                ),
                'status' => array(
                    'type' => 'INT',
                    'constraint' => 2,
                ),
            ),
            'attributes' => array('ENGINE' => 'MyISAM'),
        );
    }

    // migrating
    public function up()
    {
        // prepare migration array
        $this->setup();
        // migrating the above array
        $this->migrating();
    }

    // un-migrating
    public function down()
    {
        // prepare migration array
        $this->setup();
        // remove migration
        foreach ($this->migration as $table => $fields)
        {
            if(get_instance()->db->count_all($table) == 0)
            {
                get_instance()->dbforge->drop_table($table, TRUE);
            }
        }
    }
}
?>

For more information about migration array see Codeigniter Database Forge Class syntax.

Now execute the migrate command to create the tables in database

php creator migrate 20160907082206

Next is the Seeder.