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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <?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: */ 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