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 7: Defining Models 96<br />

Listing<br />

7-114<br />

// models/Address.php<br />

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

{<br />

public function setTableDefinition()<br />

{<br />

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

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

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

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

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

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

}<br />

}<br />

public function setUp()<br />

{<br />

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

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

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

'onDelete' => 'CASCADE'<br />

)<br />

);<br />

}<br />

Listing<br />

7-115<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 />

# schema.yml<br />

# ...<br />

Address:<br />

columns:<br />

user_id: integer<br />

address: string(255)<br />

country: string(255)<br />

city: string(255)<br />

state: string(2)<br />

postal_code: string(25)<br />

relations:<br />

User:<br />

local: user_id<br />

foreign: id<br />

onDelete: CASCADE<br />

The onDelete option is translated to proper DDL/DML statem<strong>en</strong>ts wh<strong>en</strong> <strong>Doctrine</strong> creates<br />

your tables.<br />

Note that 'onDelete' => 'CASCADE' is specified on the Address class, since the<br />

Address owns the foreign key (user_id) and database-level cascades are applied on the<br />

foreign key.<br />

Curr<strong>en</strong>tly, the only two supported database-level cascade styles are for onDelete and<br />

onUpdate. Both are specified on the side that owns the foreign key and applied to your<br />

database schema wh<strong>en</strong> <strong>Doctrine</strong> creates your tables.<br />

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

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

Saved successfully!

Ooh no, something went wrong!