0.10 User Manual
0.10 User Manual
Chapter 11 Data fixtures
Doctrine Data uses the Doctrine Parser for the dumping and loading of fixtures data so it is possible to use any of the
formats available in the Parser. Currently yml is the only fully supported format but xml and others are next.
11.1 Exporting
You can export data to fixtures file in many different formats
Listing 11.1
<?php
// A few ways exist for specifying where you export the data
// Dump to one large fixture file
$data = new Doctrine_Data();
$data->exportData('data.yml', 'yml');
// Dump to individual files. One file per model. 3rd argument true specifies to dump to individual files
$data = new Doctrine_Data();
$data->exportData('path/to/directory', 'yml', true);
?>
11.2 Importing
You can import data from fixtures files in many different formats
Listing 11.2
<?php
// Path can be in a few different formats
$path = 'path/to/data.yml'; // Path directly to one yml file
$path = array('data.yml', 'data2.yml', 'more.yml'); // Array of yml file paths
$path = array('directory1', 'directory2', 'directory3'); // Array of directories which contain yml files. It will find
all files with an extension of .yml
// Specify the format of the data you are importing
$format = 'yml'; // xml, yml, json
$models = array('User', 'Phonenumber'); // you can optionally specify an array of the models you wish to import the data
for, by default it loads data for all the available loaded models and the data that exists
$data = new Doctrine_Data();
$data->importData($path, $format, $models);
?>
11.3 Dummy Data
With Doctrine Data you can import dummy data to all your Doctrine Records
Listing 11.3
<?php
$numRecords = 3; // Number of dummy records to populate for each model
$models = array('User', 'Email'); // Models to generate dummy data for. If none specified it generates dummy data for all
loaded models.
$data = new Doctrine_Data();
$data->importDummyData($numRecords, $models);
?>
11.4 Writing
You can write your fixtures files manually and load them in to your applications. Below is a sample data.yml fixtures file.
You can also split your data fixtures file up in to multiple files. Doctrine will read all fixtures files and parse them,
then load all data.
Imagine a schema with the following relationships:
Listing 11.4
<?php
Resource hasMany Tag as Tags
Resource hasOne ResourceType as Type
ResourceType hasMany Resource as Resources
Tag hasMany Resource as Resources
?>
Listing 11.5
---
Resource:
Resource_1:
name: Doctrine Video Tutorial
Type: Video
Tags: [tutorial, doctrine, help]
Resource_2:
name: Doctrine Cheat Sheet
Type: Image
Tags: [tutorial, cheat, help]
ResourceType:
Video:
name: Video
Image:
name: Image
Tag:
tutorial:
name: tutorial
doctrine:
name: doctrine
help:
name: help
cheat:
name: cheat
You could optionally specify the Resources each tag is related to instead of specifying the Tags a Resource has.
Listing 11.6
Tag:
tutorial:
name: tutorial
Resources: [Resource_1, Resource_2]
doctrine:
name: doctrine
Resources: [Resource_1]
help:
name: help
Resources: [Resource_1, Resource_2]
cheat:
name: cheat
Resources: [Resource_1]
Here is how you would write code to load the data from that data.yml file
Listing 11.7
<?php
$data = new Doctrine_Data();
$data->importData('data.yml', 'yml');
?>
11.5 Fixtures For Nested Sets
Writing a fixtures file for a nested set tree is slightly different from writing regular fixtures files. The structure of
the tree is defined like this:
Listing 11.8
---
Category:
Category_1:
title: Categories # the root node
children:
Category_2:
title: Category 1
Category_3:
title: Category 2
children:
Category_4:
title: Subcategory of Category 2
11.6 Fixtures For I18n
Listing 11.9
Article:
Article_1:
name: Test article
Translation:
en:
title: Title of article
body: Body of article
fr:
title: French title of article
body: French body of article
Comments (0) [ add comment ]
No Comments
Add Comment