Skip to content

Commit 2b73ae8

Browse files
Added: onUpdate/onDelete to foreign key constraints
1 parent b455310 commit 2b73ae8

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

src/Loader/BaseLoader.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ public function loadContstraint($constraintNode)
163163
$foreignTable = (string) $constraintNode['foreign-table'];
164164
$foreignColumns = explode(',', (string) $constraintNode['foreign-columns']);
165165

166-
return new ForeignKeyConstraint($columns, $foreignTable, $foreignColumns, $name);
166+
$options = array();
167+
if (isset($constraintNode['onDelete'])) {
168+
$options['onDelete'] = (string) $constraintNode['onDelete'];
169+
}
170+
if (isset($constraintNode['onUpdate'])) {
171+
$options['onUpdate'] = (string) $constraintNode['onUpdate'];
172+
}
173+
174+
return new ForeignKeyConstraint($columns, $foreignTable, $foreignColumns, $name, $options);
167175
}
168176
}

tests/Loader/LoaderFactoryTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,7 @@ public function testLoadSchemaLoadsProperSchema()
7575
$this->assertEquals($projectConstraint->getForeignTableName(), 'user');
7676
$this->assertEquals($projectConstraint->getLocalColumns(), array('user_id'));
7777
$this->assertEquals($projectConstraint->getForeignColumns(), array('id'));
78+
$this->assertEquals($projectConstraint->onUpdate(), 'CASCADE');
79+
$this->assertEquals($projectConstraint->onDelete(), 'SET NULL');
7880
}
7981
}

tests/fixtures/schema.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232
<column name="user_id" type="integer" unsigned="true" />
3333
<column name="name" type="string" length="32" />
3434

35-
<constraint name="fk_users_project" columns="user_id" foreign-table="user" foreign-columns="id" />
35+
<constraint name="fk_users_project" columns="user_id" foreign-table="user" foreign-columns="id" onDelete="SET NULL" onUpdate="CASCADE" />
3636
</table>
3737
</schema>

0 commit comments

Comments
 (0)