Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to seed data in codeigniter

Tags:

How to seed data in CodeIgniter after migrating? I want to seed some pre-define users after the migration of table user once the migration completed.

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

class Migration_Users extends CI_Migration {

    public function up()
    {
        $this->load->database();
        $dbprefix = $this->db->dbprefix;
        $this->dbforge->add_field(array(
                'id' => array(
                        'type' => 'INT',
                        'constraint' =>11,
                        'unsigned' => TRUE,
                        'auto_increment' => TRUE
                ),
                'first_name' => array(
                        'type' => 'VARCHAR',
                        'constraint' => 50
                ),
                'last_name' => array(
                        'type' => 'VARCHAR',
                        'constraint' => 50,
                )
             ));
       $this->dbforge->add_key('id', TRUE);
        $this->dbforge->create_table('users');
      }

    public function down()
    {
            $this->dbforge->drop_table('users');
    }
 }
like image 778
sudipta Kumar Avatar asked May 12 '17 09:05

sudipta Kumar


1 Answers

I know this post is pretty old, but its comes up as the first result in google when searching for 'code igniter seeding in migration'. So thought I'd offer my solution.

You can quite easily perform simple $this->db->query($sql) methods within the up and down methods within the migration.

For my particular case I was seeding some static hash data, so put the values in a property of the migration and read from it in a loop, each time creating the SQL Insert query from my seed array.

Here is a redacted, reduced example of my seed migration file:

<?php

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

class Migration_Seed_hash_table extends CI_Migration {

    public function up()
    {
        foreach ($this->seedData as $seed ) {
            $sql = 'INSERT INTO hash_table VALUES '.$seed;
            $this->db->query($sql);
        }
    }

    public function down()
    {
        foreach ($this->seedData as $seed) {
            $hash = substr($seed, 2, 32);
            $sql = 'DELETE FROM hash_table WHERE hash = \''.$hash.'\'';
            $this->db->query($sql);
        }
    }

    private $seedData = array(
        '("2a96e53846b6232662fff87128061bbd", "Value A",)',
        '("7c78ebb4c223c96fa9c5e6s3f73cc28e", "Value B",)',
        '("ad2cb75a968ca5ef3255d924076e902f", "Value C",)',
    );
}

Note: The example above has had all error checking removed to stay small for posting purposes.

This of course relies on your data schema being in place (in the previous migration), and all values in your $seedData property being the latter/right hand side of your INSERT query, with all your columns specified.

But you get the idea. =)

like image 65
Dan Streeter Avatar answered Oct 13 '22 00:10

Dan Streeter