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 74<br />

Listing<br />

7-58<br />

// models/Task.php<br />

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

{<br />

public function setTableDefinition()<br />

{<br />

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

$this->hasColumn('par<strong>en</strong>t_id', 'integer');<br />

}<br />

public function setUp()<br />

{<br />

$this->hasOne('Task as Par<strong>en</strong>t', array(<br />

'local' => 'par<strong>en</strong>t_id',<br />

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

)<br />

);<br />

}<br />

}<br />

$this->hasMany('Task as Subtasks', array(<br />

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

'foreign' => 'par<strong>en</strong>t_id'<br />

)<br />

);<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 />

Listing<br />

7-59<br />

---<br />

# schema.yml<br />

# ...<br />

Task:<br />

columns:<br />

name: string(100)<br />

par<strong>en</strong>t_id: integer<br />

relations:<br />

Par<strong>en</strong>t:<br />

class: Task<br />

local: par<strong>en</strong>t_id<br />

foreign: id<br />

foreignAlias: Subtasks<br />

The above implem<strong>en</strong>tation is purely an example and is not the most effici<strong>en</strong>t way to store<br />

and retrieve hierarchical data. Check the NestedSet behavior included in <strong>Doctrine</strong> for the<br />

recomm<strong>en</strong>ded way to deal with hierarchical data.<br />

Join Table Associations<br />

Many to Many<br />

If you are coming from relational database background it may be familiar to you how many-tomany<br />

associations are handled: an additional association table is needed.<br />

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

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

Saved successfully!

Ooh no, something went wrong!