Object relational mapping - Relations - Foreign key constraints - Constraint actions.php 1.78 KB
Newer Older
hansbrix's avatar
hansbrix committed
1 2 3 4 5 6
//CASCADE//:
Delete or update the row from the parent table and automatically delete or update the matching rows in the child table. Both ON DELETE CASCADE and ON UPDATE CASCADE are supported. Between two tables, you should not define several ON UPDATE CASCADE clauses that act on the same column in the parent table or in the child table.

//SET NULL// :
Delete or update the row from the parent table and set the foreign key column or columns in the child table to NULL. This is valid only if the foreign key columns do not have the NOT NULL qualifier specified. Both ON DELETE SET NULL and ON UPDATE SET NULL clauses are supported.

lsmith's avatar
lsmith committed
7
//NO ACTION// :
hansbrix's avatar
hansbrix committed
8 9
In standard SQL, NO ACTION means no action in the sense that an attempt to delete or update a primary key value is not allowed to proceed if there is a related foreign key value in the referenced table.

lsmith's avatar
lsmith committed
10
//RESTRICT// :
hansbrix's avatar
hansbrix committed
11 12 13 14 15 16 17
Rejects the delete or update operation for the parent table. NO ACTION and RESTRICT are the same as omitting the ON DELETE or ON UPDATE clause.

//SET DEFAULT// :

In the following example we define two classes, User and Phonenumber with their relation being one-to-many. We also add a foreign key constraint with onDelete cascade action.

<code type='php'>
lsmith's avatar
lsmith committed
18
class User extends Doctrine_Record
hansbrix's avatar
hansbrix committed
19
{
lsmith's avatar
lsmith committed
20
    public function setUp()
hansbrix's avatar
hansbrix committed
21 22 23
    {
        $this->hasMany('Phonenumber', 'Phonenumber.user_id', array('onDelete' => 'cascade'));
    }
lsmith's avatar
lsmith committed
24
    public function setTableDefinition()
hansbrix's avatar
hansbrix committed
25 26 27 28 29 30
    {
        $this->hasColumn('name', 'string', 50);
        $this->hasColumn('loginname', 'string', 20);
        $this->hasColumn('password', 'string', 16);
    }
}
lsmith's avatar
lsmith committed
31
class Phonenumber extends Doctrine_Record
hansbrix's avatar
hansbrix committed
32
{
lsmith's avatar
lsmith committed
33
    public function setTableDefinition()
hansbrix's avatar
hansbrix committed
34 35 36 37 38 39
    {
        $this->hasColumn('phonenumber', 'string', 50);
        $this->hasColumn('user_id', 'integer');
    }
}
</code>