Populating your Web App with Sample Data during Development

Written by James McDonald

November 15, 2019

For years I have been using laboriously hand entered test data when developing. Perhaps because coding for me was a hobby and most of the things I was doing didn’t need oodles of sample data

But recently after doing a Laravel tutorial I was introduced to the Faker library and discovered that the CakePHP 3.x migration utility Phinx also can make use of Fakers database seeding abilities too

It’s such a relief to finally know how to take the tedium from sample data seeding away, but not only that have data that make sense for testing

<?php


use Phinx\Seed\AbstractSeed;

class PeopleSeeder extends AbstractSeed
{
    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * http://docs.phinx.org/en/latest/seeding.html
     */
    public function run()
    {
        $faker = Faker\Factory::create();
        $data = [];



        for ($i = 0; $i < 100; $i++) {
            //id, brother, firstname, lastname, created, modified, active
            $gender = $faker->randomElement(['male', 'female']);

            $data[] = [
                'active' => 1,
                'brother' => $gender === 'male' ? 1 : 0,
                'firstname'    => $faker->firstName($gender),
                'lastname'     => $faker->lastName,
                'created'       => date('Y-m-d H:i:s'),
                'modified'      => date('Y-m-d H:i:s')
            ];
        }
        // truncate people and relationships first
        $people = $this->table('people');
        $people_privileges = $this->table('people_privileges');
        $people_privileges->truncate();
        $people->truncate();

        $people->insert($data)->save();
    }
}

0 Comments

Submit a Comment

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…

Squarespace Image Export

To gain continued access to your Squarespace website images after cancelling your subscription you have several...

MySQL 8.x GRANT ALL STATEMENT

-- CREATE CREATE USER 'tgnrestoreuser'@'localhost' IDENTIFIED BY 'AppleSauceLoveBird2024'; GRANT ALL PRIVILEGES ON...

Exetel Opt-Out of CGNAT

If your port forwards and inbound and/or outbound site-to-site VPN's have failed when switching to Exetel due to their...