10.04.2018 Views

Doctrine_manual-1-2-en

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7: Defining Models 82<br />

Now if we access for example the fri<strong>en</strong>ds of Stefan Beer it would return one user 'Mikko<br />

Kosk<strong>en</strong>korva':<br />

Listing<br />

7-78<br />

// test.php<br />

// ...<br />

$beer->free();<br />

unset($beer);<br />

$user = <strong>Doctrine</strong>_Core::getTable('User')->findOneByUsername('Stefan Beer');<br />

print_r($user->Fri<strong>en</strong>ds->toArray());<br />

Now wh<strong>en</strong> you execute test.php you will see the following:<br />

Listing<br />

7-79<br />

$ php test.php<br />

Array<br />

(<br />

[0] => Array<br />

(<br />

[id] => 4<br />

[is_active] => 1<br />

[is_super_admin] => 0<br />

[first_name] =><br />

[last_name] =><br />

[username] => Mikko Kosk<strong>en</strong>korva<br />

[password] =><br />

[type] =><br />

[created_at] => 2009-01-20 16:53:13<br />

[updated_at] => 2009-01-20 16:53:13<br />

)<br />

)<br />

Foreign Key Constraints<br />

Introduction<br />

A foreign key constraint specifies that the values in a column (or a group of columns) must<br />

match the values appearing in some row of another table. In other words foreign key<br />

constraints maintain the refer<strong>en</strong>tial integrity betwe<strong>en</strong> two related tables.<br />

Say you have the product table with the following definition:<br />

Listing<br />

7-80<br />

// models/Product.php<br />

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

{<br />

public function setTableDefinition()<br />

{<br />

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

$this->hasColumn('price', 'decimal', 18);<br />

$this->hasColumn('discounted_price', 'decimal', 18);<br />

}<br />

public function setUp()<br />

{<br />

$this->hasMany('Order as Orders', array(<br />

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

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

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

Saved successfully!

Ooh no, something went wrong!