Skip to content

Commit

Permalink
Add on update/delete cascade tests
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jan 23, 2016
1 parent 332d02d commit 73e8854
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/LazyRecord/Schema/Relationship.php
Expand Up @@ -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())
Expand Down
8 changes: 8 additions & 0 deletions src/LazyRecord/SqlBuilder/MysqlBuilder.php
Expand Up @@ -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;
}

Expand Down
10 changes: 8 additions & 2 deletions tests/AuthorBooks/Model/AuthorBookSchema.php
Expand Up @@ -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')
;
}
}

Expand Down

0 comments on commit 73e8854

Please sign in to comment.