Skip to content

FuelPHP package for use the Faker PHP Library as simple as possible

Notifications You must be signed in to change notification settings

jhuriez/fuel-lbFaker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fuel LbFaker

FuelPHP package for use Faker PHP Library with FuelORM Model. This library generates fake data for you as simple as possible.

Installation

Manual

  1. Clone or download the fuel-lbFaker repository
  2. Move it in fuel/packages/
  3. Edit the config file fuel/packages/lbfaker/config/lbfaker.php
  4. Add 'lbfaker' to the 'always_load/packages' array in app/config/config.php (or call \Fuel\Core\Package::load('lbfaker'); whenever you want to use it).

Recommended : If you don't want to change the config file in fuel/packages/lbfaker/config/lbfaker.php, you can create your own config file in fuel/app/config/lbfaker.php. And copy the entirely of the original config file.

Usage

The usage is simple, once you have configured the package, use this method :

\Lb\Faker::faker($groupName);

With the group 'example' :

\Lb\Faker::faker('example');

You can use this package with a simple oil command here : https://github.com/jhuriez/fuel-lbFakerTask

Configuration

For use this package, you have to configure it.

You need to create 'group', here the example :

    'lb' => array(
        'faker' => array(
            
            // Group 'example'
            'example' => array(
                'populator' => '\Lb\Faker\Populator\FuelORM\Populator',
                'seed' => null,
                'locale' => 'en_US',
           ),
        ),
    ),

You have some optional options like "populator", "seed" and "local". For real example look the configuration file "lbfaker/config/lbfaker.php"

Configure entities

Once you have create the group in configuration file, you need to add your entities you want to populate, and their quantities (default: 1) :

                // Entities
                'entities' => array(
                    '\Model_Profile' => array(
                        'number' => 1,
                        'custom_formatters' => array(

                        ),
                    ),
                    '\Model_Person' => array(
                        'number' => 4,
                        'custom_formatters' => array(

                        ),
                    ),
                ),

Custom formatters

You can use custom formatters for each column of each entity :

                // Entities
                'entities' => array(
                    '\Model_Profile' => array(
                        'number' => 1,
                        'custom_formatters' => array(
                            'label' => array(
                                'method' => 'sentence',
                                'parameters' => 3,
                            ),
                        ),
                    ),
                    '\Model_Person' => array(
                        'number' => 1,
                        'custom_formatters' => array(
                            'age' => array(
                                'method' => 'randomNumber',
                                'parameters' => array(10, 70),
                            ),
                            'name' => array(
                                'method' => 'name',
                            ),
                            'email' => array(
                                'method' => null,
                            ),
                        ),
                    ),
                ),

Here is the list of all custom formatter method : https://github.com/fzaninotto/Faker#formatters

Note: If you use null in 'method', the column will not be populate

Use custom ORM Populator

You use Doctrine, Propel or Mandango ORM ? Just set the specific populator class in the group array :

    'lb' => array(
        'faker' => array(
            
            // Group 'example'
            'example' => array(
                
                // Other populator :
                //     \Faker\ORM\Doctrine\Populator
                //     \Faker\ORM\Mandango\Populator
                //     \Faker\ORM\Propel\Populator
                'populator' => '\Lb\Faker\Populator\FuelORM\Populator',
            ),
        ),
     ),

Complete example of configuration :

    'lb' => array(
        'faker' => array(
            
            // Group 'example'
            'example' => array(
                'populator' => '\Lb\Faker\Populator\FuelORM\Populator',
                'seed' => null,
                'locale' => 'en_US',
                // Entities
                'entities' => array(
                    '\Model_Profile' => array(
                        'number' => 1,
                        'custom_formatters' => array(
                            'label' => array(
                                'method' => 'sentence',
                                'parameters' => 3,
                            ),
                        ),
                    ),
                    '\Model_Person' => array(
                        'number' => 1,
                        'custom_formatters' => array(
                            'age' => array(
                                'method' => 'randomNumber',
                                'parameters' => array(10, 70),
                            ),
                            'name' => array(
                                'method' => 'name',
                            ),
                            'email' => array(
                                'method' => null,
                            ),
                        ),
                    ),
                ),
            ),
            // END group example
            
        ),
    ),

Column type configuration

The package use the field "data_type" of properties of the model for know the column type

Example with \Model_Profile :

<?php

class Model_Profile extends \Orm\Model
{
	protected static $_properties = array(
		'id',
		'label' => array(
                                        'data_type' => 'string'
                                    ),
		'active' => array(
                                        'data_type' => 'boolean',
                                    ),
                                    'idUser',
		'created_at',
		'updated_at',
	);

	protected static $_observers = array(
		'Orm\Observer_CreatedAt' => array(
			'events' => array('before_insert'),
			'mysql_timestamp' => false,
		),
		'Orm\Observer_UpdatedAt' => array(
			'events' => array('before_save'),
			'mysql_timestamp' => false,
		),
	);
}

FuelORM Model Relations

For the moment, only Has_One relation work. For example if you have Profile and Person entities, the Person Has_One Profil.

When fake data will be generated, Person entity will contains a fake generated Profil entity in his relation (Has_One)

Note: Profil entity need to be before the Person entity in the configuration array

Fake PHP Library

More info with the Fake PHP Library : https://github.com/fzaninotto/Faker

About

FuelPHP package for use the Faker PHP Library as simple as possible

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages