nivanka
nivanka

Reputation: 1372

codeigniter how to create tables for the models

I need to create the tables for the models i have in my code base with codeigniter,

is there a way to export the stuff to the codeignitor?

I have the codebase with me but i dont have a data dump, so I need to find a way to do something like the Rails migrations

Upvotes: 1

Views: 20052

Answers (1)

NDBoost
NDBoost

Reputation: 10634

Use CodeIgniter's Migration and DBForge class

Migrations

https://www.codeigniter.com/user_guide/libraries/migration.html

DBForge Database Manipulation

https://www.codeigniter.com/user_guide/database/forge.html

Straight From The UserGuide for migrations:

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

class Migration_Add_blog extends CI_Migration {

public function up()
{
    $this->dbforge->add_field(array(
        'blog_id' => array(
            'type' => 'INT',
            'constraint' => 5,
            'unsigned' => TRUE,
            'auto_increment' => TRUE
        ),
        'blog_title' => array(
            'type' => 'VARCHAR',
            'constraint' => '100',
        ),
        'blog_description' => array(
            'type' => 'TEXT',
            'null' => TRUE,
        ),
    ));
    
    $this->dbforge->create_table('blog');
}

public function down()
{
    $this->dbforge->drop_table('blog');
}

Upvotes: 5

Related Questions