Skip to content

Defining Relationship

Yo-An Lin edited this page Apr 27, 2017 · 2 revisions

Relationships

Belongs To

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' );

Has One

one(accessor_name, self_column_name, foreign_schema_class_name, foreign_schema_column_name)

$this->one( 'author', 'author_id', '\TestApp\AuthorSchema' , 'id' );

Has Many

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' );

Usage

$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";
}

Clone this wiki locally