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 16: Behaviors 242<br />

}<br />

}<br />

);<br />

)<br />

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

'foreign' => 'user_id'<br />

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

{<br />

public function setTableDefinition()<br />

{<br />

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

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

}<br />

}<br />

public function setUp()<br />

{<br />

$this->hasOne('User', array(<br />

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

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

)<br />

);<br />

}<br />

Listing<br />

16-8<br />

Here is the same example in YAML format. You can read more about YAML in the YAML<br />

Schema Files (page 195) chapter:<br />

---<br />

User:<br />

columns:<br />

username: string(255)<br />

password: string(255)<br />

Email:<br />

columns:<br />

address: string<br />

user_id: integer<br />

relations:<br />

User:<br />

Now if we ext<strong>en</strong>d the User and Email classes and create, for example, classes<br />

Ext<strong>en</strong>dedUser and Ext<strong>en</strong>dedEmail, the Ext<strong>en</strong>dedUser will still have a relation to the<br />

Email class and not the Ext<strong>en</strong>dedEmail class. We could of course override the setUp()<br />

method of the User class and define relation to the Ext<strong>en</strong>dedEmail class, but th<strong>en</strong> we lose<br />

the whole point of inheritance. <strong>Doctrine</strong>_Template can solve this problem elegantly with<br />

its dep<strong>en</strong>d<strong>en</strong>cy injection solution.<br />

In the following example we'll define two templates, UserTemplate and EmailTemplate,<br />

with almost id<strong>en</strong>tical definitions as the User and Email class had.<br />

Listing<br />

16-9<br />

// models/UserTemplate.php<br />

class UserTemplate ext<strong>en</strong>ds <strong>Doctrine</strong>_Template<br />

{<br />

public function setTableDefinition()<br />

{<br />

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

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

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

Saved successfully!

Ooh no, something went wrong!