Skip to content

Basics of Faker Project

Lewis Dyer edited this page Aug 12, 2015 · 13 revisions

Outline of Project Folders

A Faker Project folder has the following contents:

skelton/
├── config
├── dump
├── extension
│   ├── doctrine
│   │   ├── platforms
│   │   └── type
│   ├── faker
│   │   ├── formatter
│   │   └── type
│   └── locale
|   |__ seed
├── sources
└── template
    ├── custom
    ├── phpunit
    └── sql
        ├── db2
        ├── mysql
        ├── oracle
        ├── postgresql
        ├── sqlite
        ├── sqlserver
        ├── sqlserver2005
        └── sqlserver2008

Config Folder- This where database configs are stored.

Dump - This where the generated sql files are placed.

Template - Custom Folder for twig templates, for example the SQLFormatter use looks the the platform and under that platform a header and footer template. You add custom templates here.

Sources - Where the schema files you write are placed.

Extension - This where add new Doctrine ColumnsTypes, Formatters, CustomTypes , Locales and Seed Classes.

Where does the generated content go?

As the generator executes the content are placed into files under the dump folder using the file name settings at the time with each run overwriting the earlier.

Where place schema files?

Schema files should be added to under the source folder. If your using XMLEngine then the schema file needs to be and XML file. If your using the FluentInterface or EntityEngine the file will need a PHP extension. Quickest way to start is to copy one of the example schemas.

How to add extensions and seed classes

Custom PHP Code is added under the extension folder.

All file names must be in lowercase. Though class Names can use CamalCase.

New Doctrine ColumnTypes and Platforms should be placed under the doctrine sub folder and these new classes must be registered with relevant factories under bootstrap.php.

New Locales are to be added to the locale folder and these new classes must be registered with relevant factories under bootstrap.php.

New CustomTypes and Formatters are added to the faker folder and must be registered with relevant factories under bootstrap.php.

Seed classes must be added to seed sub folder, they are not registered with any factories. Fakers autoloader is used to require these files.

Classes must use the Faker\Extension namespace.

Running your first project.

  1. Step 1. Install from composer or install from git repo.
  2. Create a project directory in your application e.g 'faker'.
  3. Execute the ../vendor/bin/faker.php faker:init
  4. Create a schema file or copy one of the examples.
  5. Execute the generate command ../vendor/bin/faker.php faker:generate examples/basic_no_conn.php

Setup Database Configurations.

To Added a new database you can use the CLI wizard or add another connection to and existing database connection config file found under config/default.php.

Faker Supports many database conncetions it uses Doctrine DBAL to provide connection controls. The required config params for each platform can be found at DBAL Docs Page.

To Add MYSQL connection using the cli wizard execute the following command.

../vendor/bin/faker.php faker:configure

You will be asked question like the following

Which Database does this belong? [mysql|mssql|oracle|posgsql]: mysql
What is the Database schema name? : sakila
What is the Database user name? : root
etc ....

Every connection needs a unique connection name this name is used to map a connection to a Datasource. Faker can also write generated SQL directly to database using connections references by this name.

If this is your first connection the file config/default.php will be written with this database config. If you run this command again another name connection is added.

The wizard may not always ask for every required connection param, You may need to modify the config file to add them.

Analyse A Database Schema

This command will build a simple XML schema file from existing database. I wrote this command when I still believed XML was the best way to describe a schema but I have since changed direction preferring SeedClass and FluentEngine.

 ./vendor/bin/faker.php faker:analyse 

This command will output a file called schema.xml to the sources directory. This schema will have all tables in the database listed as well as a default writer setup ready to use. The alphanumeric filler type will be applied to each column (alpahnumeric).