Skip to content

Commit

Permalink
belongsTo $foreignClass shouldn't be optional
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jun 15, 2016
1 parent 4f9c3df commit b5c9f80
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Schema/DeclareSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ protected function getCurrentSchemaClass()
* @param string $foreignColumn foreign reference schema column.
* @param string $selfColumn self column name
*/
public function belongsTo($accessor, $foreignClass = NULL, $foreignColumn = NULL, $selfColumn = 'id')
public function belongsTo($accessor, $foreignClass, $foreignColumn = 'id', $selfColumn = null)
{
if ($foreignClass && NULL === $foreignColumn) {
$s = new $foreignClass;
Expand Down
9 changes: 9 additions & 0 deletions src/Schema/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ public function __construct($accessor, array $data = array())
$this->data = $data;
}


public function by($column)
{
$this->data['self_column'] = $column;
return $this;
}



public function newForeignSchema()
{
$class = $this->data['foreign_schema'];
Expand Down
7 changes: 3 additions & 4 deletions tests/AuthorBooks/Model/AddressSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ public function schema()
->boolean()
->defaultValue(false)
;
/**
* $address->author
*/
$this->belongsTo('author', '\\AuthorBooks\Model\\AuthorSchema', 'id' , 'author_id')

$this->belongsTo('author', '\\AuthorBooks\Model\\AuthorSchema')
->by('author_id')
->onDelete('CASCADE');
}
}

0 comments on commit b5c9f80

Please sign in to comment.