Welcome Guest, Not a member yet? Register   Sign In
database seeding
#1

i am trying to find some libraries for database seeding
i found one
can any body help how to use this with codeigniter

https://github.com/fzaninotto/Faker
Reply
#2

The biggest issue here is that CI doesn't have the concept of Seeders built in currently. However, it's really simple to build one since it's simply a class that runs a method that does whatever you want it to. Basically a bunch of libraries. Sprint has one built in that is meant to run from the CLI, but you could easily modify this system to work through the browser.

Faker, which is an excellent library, simply creates the data for you. Here's an example from a seed on a project that I'm currently working on to give you an idea how to use it.

Code:
<?php

use App\Music\Album;
use App\Music\Track;

/**
* Class FakeMusicSeeder
*
* Creates a good selection of music tracks and related information for
* use when developing the site.
*/
class FakeMusicSeeder extends Seeder {

   protected $moods = ['Angry', 'Carefree', 'Chill', 'Contemplative', 'Ecstatic', 'Eerie', 'Happy', 'Love', 'Peaceful',
                       'Sad', 'Serious', 'Somber', 'Tense', 'Uplifting'];

   protected $keywords = ['Acoustic', 'Aggressive', 'Beach', 'Bouncy', 'Celtic', 'Dance', 'Dark', 'Dramatic', 'Earthy',
                           'Enchanted', 'Epic', 'Grungy', 'Gypsy', 'Holiday', 'Industrial', 'Mysterious', 'Quirky',
                           'Suspense', 'Vintage'];

   protected $genres = ['Alternative', 'Ambient', 'Blues', 'Cinematic', 'Classical', 'Country', 'Electronic', 'Folk',
                       'Hip Hop', 'Indie', 'Jazz', 'Pop', 'Post Rock', 'Rock', 'Singer-Songwriter', 'Soul', 'World'];

   protected $instruments = ['Acoustic Guitar', 'Banjo', 'Cello', 'Drums', 'Fiddle', 'Harmonica', 'Horns', 'Humming',
                           'Mandolin', 'Nature Sounds', 'Organ', 'Percussion', 'Piano', 'Rhodes', 'Sound FX'];

   protected $track_count = 10;

   protected $album_count = 1;

   protected $faker;

   //--------------------------------------------------------------------

   public function __construct()
   {
       $this->faker = Faker\Factory::create();
       $this->faker->addProvider(new Faker\Provider\Company($this->faker));
       $this->faker->addProvider(new Faker\Provider\Uuid($this->faker));
       $this->faker->addProvider(new Faker\Provider\Color($this->faker));
       $this->faker->addProvider(new Faker\Provider\DateTime($this->faker));
       $this->faker->addProvider(new Faker\Provider\Lorem($this->faker));
       $this->faker->addProvider(new Faker\Provider\en_US\Person($this->faker));
   }

   //--------------------------------------------------------------------


   public function run()
   {
       for ($i=0; $i < $this->album_count; $i++)
       {
           $album = $this->createAlbum();

           for ($j=0; $j < $this->track_count; $j++)
           {
               try {
                   $track = $this->createTrack();
               }
               catch (Exception $e)
               {
//                    echo "ERROR: ". $e->getMessage();
                   continue;
               }

               $album->addTrack($track, $j);
           }
       }
   }

   //--------------------------------------------------------------------

   protected function createAlbum()
   {
       $album = new Album();

       $album->setUUID( $this->faker->uuid );
       $album->setCode( $this->faker->bothify('??####') );
       $album->setName( $this->faker->catchPhrase );
       $album->setStatus( 'active' );
       $album->setFeatured( false );
       $album->setReleaseDate( $this->faker->dateTimeThisDecade() );
       $album->setOverlayColor( $this->faker->hexColor );

       // @todo Create an image for the album cover....

       $album->save();

       return $album;
   }

   //--------------------------------------------------------------------

   protected function createTrack()
   {
       $track = new Track();

       $track->setUUID( $this->faker->uuid );
       $track->setTitle( $this->faker->catchphrase );
       $track->setDescription( $this->faker->text );
       $track->setBPM( $this->faker->numberBetween(60, 180) );
       $track->setPurchaseCount( $this->faker->numberBetween(1, 1000) );
       $track->setPlayCount( $this->faker->numberBetween(1, 2500) );
       $track->setLengthInSeconds( $this->faker->numberBetween(60, 360) );
//        $track->setFrequency();
//        $track->setBitrate();
//        $track->setTempo();
//        $track->setLyrics();
//        $track->setVersion();
//        $track->setMixout();
       $track->setStatus('active');

       $track->addArtists( $this->faker->name );
       $track->addGenres( $this->faker->randomElements($this->genres, 2) );
       $track->addInstruments( $this->faker->randomElements($this->instruments, 3) );
       $track->addKeywords( $this->faker->randomElements($this->keywords, 2) );
       $track->addMoods( $this->faker->randomElements($this->moods, 2) );

       $track->save();

       return $track;
   }

   //--------------------------------------------------------------------

}
Reply
#3

thanks this help a lot
Reply
#4

(This post was last modified: 07-06-2015, 03:11 PM by kenjis.)

Yes, Seeder is simple, and Faker is a good library.

Here is my database seeder with Faker:
https://github.com/kenjis/codeigniter-te...Seeder.php
https://github.com/kenjis/codeigniter-te...Seeder.php
Reply
#5

Is there a way to generate large test data, i.e million records for testing? example a file with name , phone number , zipcode ?

thanks
Reply




Theme © iAndrew 2016 - Forum software by © MyBB