diff --git a/src/LazyRecord/Schema/Relationship.php b/src/LazyRecord/Schema/Relationship.php index 478811ca7..70aedb246 100644 --- a/src/LazyRecord/Schema/Relationship.php +++ b/src/LazyRecord/Schema/Relationship.php @@ -34,9 +34,9 @@ class Relationship implements IteratorAggregate, ArrayAccess public $orderBy = array(); - protected $onUpdate; + public $onUpdate; - protected $onDelete; + public $onDelete; public function __construct($accessor, array $data = array()) diff --git a/src/LazyRecord/SqlBuilder/MysqlBuilder.php b/src/LazyRecord/SqlBuilder/MysqlBuilder.php index cfafcbf5f..37cd30b7e 100644 --- a/src/LazyRecord/SqlBuilder/MysqlBuilder.php +++ b/src/LazyRecord/SqlBuilder/MysqlBuilder.php @@ -66,10 +66,18 @@ public function buildColumnSql(SchemaInterface $schema, DeclareColumn $column) $fColumn = $rel['foreign_column']; $fc = $fSchema->columns[$fColumn]; $sql .= ' REFERENCES ' . $fSchema->getTable() . '(' . $fColumn . ')'; + + if ($rel->onUpdate) { + $sql .= ' ON UPDATE ' . $rel->onUpdate; + } + if ($rel->onDelete) { + $sql .= ' ON DELETE ' . $rel->onDelete; + } } break; } } + echo $sql, PHP_EOL; return $sql; } diff --git a/tests/AuthorBooks/Model/AuthorBookSchema.php b/tests/AuthorBooks/Model/AuthorBookSchema.php index cc5814999..244f2d326 100644 --- a/tests/AuthorBooks/Model/AuthorBookSchema.php +++ b/tests/AuthorBooks/Model/AuthorBookSchema.php @@ -19,8 +19,14 @@ function schema() ->integer() ->required(); - $this->belongsTo('book','\AuthorBooks\Model\BookSchema','id','book_id'); - $this->belongsTo('author', '\AuthorBooks\Model\AuthorSchema' , 'id', 'author_id'); + $this->belongsTo('book','\AuthorBooks\Model\BookSchema','id','book_id') + ->onDelete('CASCADE') + ->onUpdate('CASCADE') + ; + $this->belongsTo('author', '\AuthorBooks\Model\AuthorSchema' , 'id', 'author_id') + ->onDelete('CASCADE') + ->onUpdate('CASCADE') + ; } }