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

# ...<br />

relations:<br />

# ...<br />

Addresses:<br />

class: Address<br />

local: id<br />

foreign: user_id<br />

cascade: [delete]<br />

The cascade option is used to specify the operations that are cascaded to the related objects<br />

on the application-level.<br />

Please note that the only curr<strong>en</strong>tly supported value is delete, more options will be added<br />

in future releases of <strong>Doctrine</strong>.<br />

In the example above, <strong>Doctrine</strong> would cascade the deletion of a User to it's associated<br />

Addresses. The following describes the g<strong>en</strong>eric procedure wh<strong>en</strong> you delete a record through<br />

$record->delete():<br />

1. <strong>Doctrine</strong> looks at the relations to see if there are any deletion cascades it needs to apply. If<br />

there are no deletion cascades, go to 3).<br />

2. For each relation that has a delete cascade specified, <strong>Doctrine</strong> verifies that the objects that<br />

are the target of the cascade are loaded. That usually means that <strong>Doctrine</strong> fetches the related<br />

objects from the database if they're not yet loaded.(Exception: many-valued associations are<br />

always re-fetched from the database, to make sure all objects are loaded). For each<br />

associated object, proceed with step 1).<br />

3. <strong>Doctrine</strong> orders all deletions and executes them in the most effici<strong>en</strong>t way, maintaining<br />

refer<strong>en</strong>tial integrity.<br />

From this description one thing should be instantly clear: Application-level cascades happ<strong>en</strong><br />

on the object-level, meaning operations are cascaded from one object to another and in order<br />

to do that the participating objects need to be available.<br />

This has some important implications:<br />

• Application-level delete cascades don't perform well on many-valued associations<br />

wh<strong>en</strong> there are a lot of objects in the related collection (that is because they need to<br />

be fetched from the database, the actual deletion is pretty effici<strong>en</strong>t).<br />

• Application-level delete cascades do not skip the object lifecycle as database-level<br />

cascades do (see next chapter). Therefore all registered ev<strong>en</strong>t list<strong>en</strong>ers and other<br />

callback methods are properly executed in an application-level cascade.<br />

Database-Level Cascades<br />

Some cascading operations can be done much more effici<strong>en</strong>tly at the database level. The best<br />

example is the delete cascade.<br />

Database-level delete cascades are g<strong>en</strong>erally preferrable over application-level delete<br />

cascades except:<br />

• Your database does not support database-level cascades (i.e. wh<strong>en</strong> using MySql with<br />

MYISAM tables).<br />

• You have list<strong>en</strong>ers that list<strong>en</strong> on the object lifecycle and you want them to get<br />

invoked.<br />

Database-level delete cascades are applied on the foreign key constraint. Therefore they're<br />

specified on that side of the relation that owns the foreign key. Picking up the example from<br />

above, the definition of a database-level cascade would look as follows:<br />

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

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

Saved successfully!

Ooh no, something went wrong!