-
-
Notifications
You must be signed in to change notification settings - Fork 2
5. Relationships
In relational databases, tables are related to one another. There are several types of relationships that can be defined. This framework makes it easy to define and use relationships without having to write extensive code.
There are 3 types of relationships defined in this framework: One-to-One
, One-to-Many
and Many-to-Many
.
A One-to-One
relationship is the simplest relationship between two entities. For example, we can have an authorization module where every User
has one Account
and reciprocally every Account
has one User
. This is a typical One-to-One
relationship.
In order to create such a relationship, you first need to make sure that the database tables that you create for the two entities both have foreign keys that link to one another.
Learn more about
Relational Databases
here
Once your database tables are created, one of the models User
, for example, will add a new method that invokes the hasOne()
method to define that relationship.
class User extends Model {
// properties go here
//...
//new method that defines a One-to-One relationship
public function account()
{
return $this->hasOne('Account','accounts');
// the first argument is the model name to link to
// the second argument is the database table name of to link to
}
}
In the Account
model, you will need to do the same using the hasOne
method.
class Account extends Model {
// properties go here
//...
//new method that defines the inverse of a One-to-One relationship
public function user()
{
return $this->hasOne('User','users');
// the first argument is the model name to link to
// the second argument is the database table name of to link to
}
}
Notice: In both the examples, there is no restriction as to the name of the method used to define the relationships. We recommend using a name that is representative enough of the type of relationship.
After the relationship has been defined in the respective models, making use of that relationship takes place in the context of the Controller
. For example, in order to get the Account
associated with a User
, you would do the following in the UserController
:
class UserController extends Controller {
// Assuming $user has been created
$account = $user->find(1)->account(); // returns account for user with id 1
}
A One-to-Many
relationship is a common type of relationship between two entities. Taking the same example as before, you could allow one User
to have multiple Account
but an Account
to belong to one user. For that you would want to use a One-to-Many
relationship.
In order to create such a relationship, you first need to make sure that the entity that could exist many times has a reference in the table of the other entity. In our example, that means that there should be a foreign_key
in the accounts
table that references the users
table.
Learn more about
Relational Databases
here
Once your database tables are created, the User
model needs a new method that invokes the hasMany()
method to define that relationship.
class User extends Model {
// properties go here
//...
//new method that defines a One-to-Many relationship
public function accounts()
{
return $this->hasMany('Account','accounts');
// the first argument is the model name to link to
// the second argument is the database table name of to link to
}
}
In the Account
model, the account belongs to one User
. You will need to define a belongTo
relationship.
class Account extends Model {
// properties go here
//...
//new method that defines the inverse of a One-to-One relationship
public function user()
{
return $this->belongsTo('User','users');
// the first argument is the model name to link to
// the second argument is the database table name of to link to
}
}
Notice: In both the examples, there is no restriction as to the name of the method used to define the relationships. We recommend using a name that is representative enough of the type of relationship.
After the relationship has been defined in the respective models, making use of that relationship takes place in the context of the Controller
. For example, in order to get all the accounts associated with a User
, you would do the following in the UserController
:
class UserController extends Controller {
// Assuming $user has been created
$account = $user->find(1)->accounts(); // returns an Array of Accounts of all the accounts associated to user 1
}