2017 © Pedro Peláez
 

library paris-seeder

A simple database seeding class using Faker data and Paris models

image

brad-denver/paris-seeder

A simple database seeding class using Faker data and Paris models

  • Thursday, September 4, 2014
  • by BradDenver
  • Repository
  • 1 Watchers
  • 1 Stars
  • 26 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

paris-seeder

A simple database seeding class using Faker data and Paris models, (*1)

Example Syntax, (*2)

Seeder::seed('Seeds\Roles', 2, null, function($record) {
  Seeder::seed('Seeds\Users', 2, array('role_id'=>$record->id));
});

The above example would create two roles and two users with each role., (*3)

Install and config

The easiest way to install paris-seeder and its dependencies (faker, idiorm, paris) is via Composer. This class is available through Packagist with the vendor and package identifier of brad-denver/paris-seeder., (*4)

Paris-seeder does not require any configuration itself but the following steps show how to configure Idiorm and setup Paris models for its use., (*5)

An example Idiorm config, (*6)

ORM::configure('mysql:host=localhost;dbname=my_database');
ORM::configure('username', 'database_user');
ORM::configure('password', 'top_secret');

Examples

Lets assume we are going to seed a roles table, (*7)

id title
1 Boss
2 Worker

and a users table, (*8)

id name role_id
1 Sally Hard 2
2 Bob Lazy 1

both with auto incrementing id fields., (*9)

Paris Models

fist we need to create classes for each table that extends the Paris Model class, (*10)

namespace Seeds;

class Roles extends \Model {

  /*
  * use the Paris filter pattern to create a new fake record
  */
  public function create_fake($orm, $faker) {
    $orm->create(array(
      'title' => $faker->word
    ));
    return $orm;
  }
}

class Users extends \Model {

  /*
  * use the Paris filter pattern to create a new fake record
  */
  public function create_fake($orm, $faker) {
    $orm->create(array(
      'name' => $faker->name,
      'role_id' => $faker->randomDigit
    ));
    return $orm;
  }
}

The key thing here is that the models have a create_fake method that accepts an Idiorm ORM instance and Faker\Generator instance and returns the record resulting from $orm->create., (*11)

Seeder::seed

The seed method expects: * a paris model instance (or the string/s to create one) * the count of records to insert (defaults to 1) * optional data to overide that provided by faker in create_fake * optional callback to be called for record that is inserted (it will be passed the new rocord and the faker instance) * optional Faker/Generator instance to generate fake data (if omitted a new instance will be created) A basic example., (*12)

Seeder::seed('Seeds\Users', 5);

Overide faker data., (*13)

Seeder::seed('Seeds\Users', 5, array('role_id'=>2));

Suppling a callback., (*14)

Seeder::seed('Seeds\Roles', 2, null, function($record) {
  Seeder::seed('Seeds\Users', 5, array('role_id'=>$record->id));
});

Suppling a faker generator, (*15)

$faker = Faker\Factory::create('fr_FR'); // create a French faker
Seeder::seed('Seeds\Users', 5, null, null, $faker);

Seeder::replicate

Sometimes there may be no need to use fake data for a certain table. Seeder::replicate is helper method to copy all data from one table to another (assuming they have compatible schemas)., (*16)

// a paris model pointing to our production roles table
$source_model = Model::factory('Roles', 'remote');
// a paris model pointing to our dev roles table that needs to mirror production
$target_model = Model::factory('Roles', 'local');
Seeder::replicate($source_model, $target_model);

Seeder::delete_all

as its name suggests this method simply deletes all records for given models table. It is called as the first step of Seeder::replicate and Seeder::delete_all_and_seed, (*17)

Seeder::delete_all('Seeds\Users');

Seeder::delete_all_and_seed

a helper method for the common use case of deleting and reseeding all data in a table. This method simply calls Seeder::delete_all followed by Seeder:seed for the given model. It accepts the same arguments as Seeder:seed, (*18)

Seeder::delete_all_and_seed('Seeds\Users', 5);

The Versions

04/09 2014

dev-master

9999999-dev https://github.com/BradDenver/paris-seeder

A simple database seeding class using Faker data and Paris models

  Sources   Download

MIT

The Requires

 

orm faker seed idiorm paris

03/09 2014

v0.0.1

0.0.1.0 https://github.com/BradDenver/paris-seeder

A simple database seeding class using Faker data and Paris models

  Sources   Download

MIT

The Requires

 

orm faker seed idiorm paris

02/09 2014

0.0.0

0.0.0.0 https://github.com/BradDenver/paris-seeder

A simple database seeding class using Faker data and Paris models

  Sources   Download

MIT

The Requires

 

orm faker seed idiorm paris