data-fixtures.txt 3.76 KB
Newer Older
Jonathan.Wage's avatar
Jonathan.Wage committed
1 2 3 4 5 6 7 8 9 10
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.

++ Exporting

You can export data to fixtures file in many different formats

<code type="php">
// A few ways exist for specifying where you export the data

// Dump to one large fixture file
11 12
$data = new Doctrine_Data();
$data->exportData('data.yml', 'yml');
Jonathan.Wage's avatar
Jonathan.Wage committed
13 14

// Dump to individual files. One file per model. 3rd argument true specifies to dump to individual files
15 16
$data = new Doctrine_Data();
$data->exportData('path/to/directory', 'yml', true);
Jonathan.Wage's avatar
Jonathan.Wage committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
</code>

++ Importing

You can import data from fixtures files in many different formats

<code type="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';
$format = 'xml';
$format = 'csv';

$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

36 37
$data = new Doctrine_Data();
$data->importData($path, $format, $models);
Jonathan.Wage's avatar
Jonathan.Wage committed
38 39
</code>

40 41 42 43 44 45 46 47
++ Dummy Data

With Doctrine Data you can import dummy data to all your Doctrine Records

<code type="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.

48 49
$data = new Doctrine_Data();
$data->importDummyData($numRecords, $models);
50 51
</code>

Jonathan.Wage's avatar
Jonathan.Wage committed
52 53
++ Writing

54 55 56
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.

Please see the [doc schema-files :index :name] for the sample models/schema for these example fixtures.
Jonathan.Wage's avatar
Jonathan.Wage committed
57 58 59

<code type="yml">
---
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
Adult:
  Adult_1:
    name: Parent 1
    Contact: Contact_1
Car:
  Car_1:
    name: Chevorlet Trailblazer
  Car_2:
    name: Chevorlet Blazer
  Car_3:
    name: Buick
Child:
  Child_1:
    name: Child 1
    Adult: Adult_1
Contact:
  Contact_1:
Jonathan.Wage's avatar
Jonathan.Wage committed
77
    name: Jonathan H. Wage
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  Contact_3:
    name: Daniel Adams
  Contact_4:
    name: Robert Adams
Dog:
  Dog_1:
    name: Sam
    User: User_1
  Dog_2:
    name: Dixie
    User: User_2
  Dog_3:
    name: Chief
    User: User_3
SelfReference:
  SelfReference_1:
    User1: User_1
    User2: User_2
    name: Self Reference 1
    SelfReference1: SelfReference_2
    SelfReference2: SelfReference_3
  SelfReference_2:
    User1: User_2
    User2: User_1
    name: Self Reference 2
    SelfReference1: SelfReference_1
    SelfReference2: SelfReference_1
  SelfReference_3:
    User1: User_2
    User2: User_1
    name: Self Reference 3
    SelfReference1: SelfReference_3
    SelfReference2: SelfReference_3
User:
  User_1:
    username: jwage
    hair_color: light brown
    Contact: Contact_1
  User_2:
    username: dadams
    hair_color: dark brown
    Contact: Contact_3
  User_3:
    username: radams
    hair_color: light brown
    Contact: Contact_4
UserCar:
  UserCar_1:
Jonathan.Wage's avatar
Jonathan.Wage committed
126
    User: User_1
127 128 129 130 131 132 133
    Car: Car_1
  UserCar_2:
    User: User_2
    Car: Car_2
  UserCar_3:
    User: User_3
    Car: Car_3
Jonathan.Wage's avatar
Jonathan.Wage committed
134 135 136 137 138
</code>

Here is how you would write code to load the data from that data.yml file

<code type="php">
139 140
$data = new Doctrine_Data();
$data->importData('data.yml', 'yml');
Jonathan.Wage's avatar
Jonathan.Wage committed
141
</code>