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();
    }
}