Skip to content

Seeding

As most XELOS functions are dependent on the database and a certain set of configuration and data settings, we need to make sure to create a static setting before running the specific tests. This can be done by using Seeds. The framework as well as modules may provide a set of seeds in their configuration which can be loaded before test execution. In future releases seeding will also be supported during the creation of new module instances to provide users a basic instance configuration.

Seeds are located in the setup/seeds/[seed_name]/[seed_script].php of the system or the module, where [seedname] is the name of the specific seed (e.g. base) and [seed_script] is the specific script to load the required data. Each seed can consist of multiple scripts which will all be called during the seeding. For best readability we recommend using one script per model / table, e.g. user.php to fill the user model and policy.php to fill create the policy models.

Note

Please use one seed script per model. Do not modify or add data outside of your specific instance.

Helper

XELOS provides helper classes to facilitate the seeding process.

CSV Import

The CSV Import helper can be used to create models based on simple CSV files. The helper loads the CSV, loops through the rows and creates a new model per row. All columns are being set as property. Before and after callbacks give additional flexibility to call custom model functions or to perform manual property transformations. Finally, each model is being saved causing all of the model's implemented validations and submodel generations to be processed accordingly.

A simple CSV based seed could look as follows:

CSV File: /setup/seeds/base/data/user.csv

firstname;lastname;email
Daniel;Wilhelm;daniel@xelos.net
Stefan;Pasel;stefan@xelos.net

Seed Script: /setup/seeds/base/user.php

<?php
use XELOS\Framework\Lib\Installer\Seed\CsvImport;

// Initialize System Groups
// $seed_instance is automatically passed to the script from the seeding logic.
// It contains the specific instance object.

$seed = new CsvImport($seed_instance, 'user');
$seed->set_source(dirname(__FILE__).'/data/user.csv');
$seed->import();

Fake Import

The Fake Import class provides a wrapper to the Faker helper which can be used to generate automated test data. This is especially usefull for example and / or load testing seed data.

Manual

You can also use manual database functions or other methods during the seeding process. E.g. using the auto_query function:

<?php
$XF->db->auto_query('insert', 'my_table', array(
    'firstname'=>'John',
    'lastname'=>'Wayne',
    'age'=>18
));