10.04.2018 Views

Doctrine_manual-1-2-en

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 19: Data Fixtures 286<br />

Listing<br />

19-6<br />

Implem<strong>en</strong>t<br />

Now that we know a little about data fixtures lets implem<strong>en</strong>t them in to our test <strong>en</strong>vironm<strong>en</strong>t<br />

we created and have be<strong>en</strong> using through the previous chapters so that we can test the<br />

example fixtures used in the next sections.<br />

First create a directory in your doctrine_test directory named fixtures and create a file<br />

named data.yml inside:<br />

$ mkdir fixtures<br />

$ touch fixtures/data.yml<br />

Now we need to just modify our g<strong>en</strong>erate.php script to include the code for loading the<br />

data fixtures. Add the following code to the bottom of g<strong>en</strong>erate.php:<br />

Listing<br />

19-7<br />

// g<strong>en</strong>erate.php<br />

// ...<br />

<strong>Doctrine</strong>_Core::loadData('fixtures');<br />

Writing<br />

You can write your fixtures files <strong>manual</strong>ly and load them in to your applications. Below is a<br />

sample data.yml fixtures file. You can also split your data fixtures file up in to multiple files.<br />

<strong>Doctrine</strong> will read all fixtures files and parse them, th<strong>en</strong> load all data.<br />

For the next several examples we will use the following models:<br />

Listing<br />

19-8<br />

// models/Resouce.php<br />

class Resource ext<strong>en</strong>ds <strong>Doctrine</strong>_Record<br />

{<br />

public function setTableDefinition()<br />

{<br />

$this->hasColumn('name', 'string', 255);<br />

$this->hasColumn('resource_type_id', 'integer');<br />

}<br />

public function setUp()<br />

{<br />

$this->hasOne('ResourceType as Type', array(<br />

'local' => 'resource_type_id',<br />

'foreign' => 'id'<br />

)<br />

);<br />

}<br />

}<br />

$this->hasMany('Tag as Tags', array(<br />

'local' => 'resource_id',<br />

'foreign' => 'tag_id',<br />

'refClass' => 'ResourceTag'<br />

)<br />

);<br />

// models/ResourceType.php<br />

----------------- Brought to you by

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!