-
Notifications
You must be signed in to change notification settings - Fork 26
Defining Relationship
Yo-An Lin edited this page Apr 27, 2017
·
2 revisions
belongsTo(accessor_name, foreign_schema_class_name, foreign_schema_column_name, self_column_name = 'id')
$this->belongsTo( 'author' , '\TestApp\AuthorSchema', 'id' , 'author_id' );
$this->belongsTo( 'address' , '\TestApp\AddressSchema', 'address_id' );one(accessor_name, self_column_name, foreign_schema_class_name, foreign_schema_column_name)
$this->one( 'author', 'author_id', '\TestApp\AuthorSchema' , 'id' );many(accessor_name, foreign_schema_class_name, foreign_schema_column_name, self_column_name )
$this->many('addresses', '\TestApp\AddressSchema', 'author_id', 'id');
$this->many('author_books', '\TestApp\AuthorBookSchema', 'author_id', 'id');To define many to many relationship:
$this->manyToMany( 'books', 'author_books' , 'book' );$address = $author->addresses->create([
'address' => 'farfaraway'
]);
$ret = $address->delete();
// create related address
$author->addresses[] = ['address' => 'Harvard'];
$addresses = $author->addresses->items(); // array of Address
foreach ($author->addresses as $address ) {
echo $address->address , "\n";
}