Database Seeding in CodeIgniter

When developing web applications, have you found yourself registering test accounts, inserting test posts, comments or otherwise inserting data directly into your database, such that you are reliant on that data?  Are you using a clone of a production database, or worse yet, developing and testing in a production environment?

Your doing it wrong!

Database seeding as a concept means that not only can you build a viable database in a flash, but crucially you can reset the data anytime without consequence as you inevitably make a meal of the existing data during application development.

Save time – fake it

You are probably only attached to your development database because you’ve spent a lot of time generating all that data and getting it just right. But theres a better way – fake it! You (probably) don’t need “actual” data in your database, you need data that is representational of what your production data will look like.

So how do we seed a database using CodeIgniter? Read on for the big reveal.

Step 1. Get Composer

If you don’t have Composer, get Composer. Seriously. If you don’t know what Composer is, start learning – you’ll never look back. I won’t be going into the installation or usage of Composer itself.

Step 2. Add the Composer autoloader to CodeIgniter

Adding composer inside CodeIgniter is as easy as adding a couple of lines to your front controller, which by default is your index.php file. Scroll to the bottom of index.php and add the following:


/*
 * --------------------------------------------------------------------
 * LOAD THE COMPOSER AUTOLOAD FILE
 * --------------------------------------------------------------------
 */
include_once '../../vendor/autoload.php';

/*
 * --------------------------------------------------------------------
 * LOAD THE BOOTSTRAP FILE
 * --------------------------------------------------------------------
 *
 * And away we go...
 *
 */
require_once BASEPATH.'core/CodeIgniter.php';

/* End of file index.php */
/* Location: ./index.php */

Step 3: Get Faker

Faker is your new best friend (if it’s possibly to be friends with a software package), and it is going to do all the heavy lifting to generate fake data.

Besides Faker providing a large array of representational user data like usernames, names, email addresses, postal addresses and credit card numbers, it also comes packaged with versatile date helpers and even a cool fake (but real) imaging service, Lorem Pixel.

Add Faker into your composer.json file. You probably want it under “require-dev” as you won’t be using it in a production environment. Note that at the time of writing the “dev-master” branch was required due to an apparent tagging mishap on the repository causing the image provider to be unavailable.

{
    "require-dev": {
        "fzaninotto/Faker": "dev-master"
    }
}

Don’t forget to update composer.

composer update

Ok, now that we have Composer, Faker and CodeIgniter singing in harmony, it’s time to get faker than Steve Tylers face! (sorry)

Step 4: Start Faking

Here’s an example controller that first truncates the old user table, and then seeds new rows into an empty user table. It is assumed that your user table already exists.

Danger: This script will truncate your user table so DO NOT run it against your production database. You have been warned.

<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

/**
 * App Class
 *
 * Stop talking and start faking!
 */
class App extends CI_Controller
{
    function __construct()
    {
        parent::__construct();

        // can only be called from the command line
        if (!$this->input->is_cli_request()) {
            exit('Direct access is not allowed');
        }

        // can only be run in the development environment
        if (ENVIRONMENT !== 'development') {
            exit('Wowsers! You don\'t want to do that!');
        }

        // initiate faker
        $this->faker = Faker\Factory::create();

        // load any required models
        $this->load->model('user_model');
    }

    /**
     * seed local database
     */
    function seed()
    {
        // purge existing data
        $this->_truncate_db();

        // seed users
        $this->_seed_users(25);

        // call more seeds here...
    }

    /**
     * seed users
     *
     * @param int $limit
     */
    function _seed_users($limit)
    {
        echo "seeding $limit users";

        // create a bunch of base buyer accounts
        for ($i = 0; $i < $limit; $i++) {
            echo ".";

            $data = array(
                'username' => $this->faker->unique()->userName, // get a unique nickname
                'password' => 'awesomepassword', // run this via your password hashing function
                'firstname' => $this->faker->firstName,
                'surname' => $this->faker->lastName,
                'address' => $this->faker->streetAddress,
                'city' => $this->faker->city,
                'state' => $this->faker->state,
                'country' => $this->faker->country,
                'postcode' => $this->faker->postcode,
                'email' => $this->faker->email,
                'email_verified' => mt_rand(0, 1) ? '0' : '1',
                'phone' => $this->faker->phoneNumber,
                'birthdate' => $this->faker->dateTimeThisCentury->format('Y-m-d H:i:s'),
                'registration_date' => $this->faker->dateTimeThisYear->format('Y-m-d H:i:s'),
                'ip_address' => mt_rand(0, 1) ? $this->faker->ipv4 : $this->faker->ipv6,
            );

            $this->user_model->insert($data);
        }

        echo PHP_EOL;
    }

    private function _truncate_db()
    {
        $this->user_model->truncate();
    }
}

You will notice a couple of checks at the top to make this script only accessible by the command line, and to restrict this script to the development environment only. If you prefer you can run this script from your browser, just comment out the command line check and run mysite.com/app/seed.

I prefer to run my scripts from the terminal, to run this script cd into your CodeIgniter folder and run:


stevethomas$  php index.php app seed

This should give you a decent foundation to start seeding your development database. The more up to date, representational and flexible your seeding process is, the faster you can rinse and repeat whether it be for testing or just when you’ve made a mess of your development database.

Further Reading

4 Replies to “Database Seeding in CodeIgniter”

  1. Thank you so much for this! I actually lost a lot of time with the uoapld library. Turns out Codeigniter version 2.1.0 has a bug in it, hope you don’t mind me linking to the page . If you get a The filetype you are attempting to uoapld is not allowed.’ and you have chmodded your directory, made sure it exists, you might have a problem MIME type detection error.Thanks again.

Leave a Reply

Your email address will not be published. Required fields are marked *