I have successfully created a few relations in a Model. This is the user-model
<?php
class User extends BaseModel{
protected $table = 'users';
public function group(){
return $this -> belongsTo('UserGroup');
}
...
}
?>
and this is the UserGroup-model (UserGroup.php)
<?php class UserGroup extends BaseModel{ ... } ?>
Every user can be in one group (the database-column is called 'group_id' in the users-table). If I want to do eager loading on this relation, it works perfectly fine, also for other models.
The problem is that I have a few models that have a lot of foreign keys and I don't want to create all relations manually. I want a function in the BaseModel that creates all those relations automatically, like
public function group(){
return $this->belongsTo('Group');
}
based on an array that I would provide in each model, looking like this
protected $foreignKeys = array(array('key' => 'group', 'model' => 'UserGroup'), ...);
I have read that there is an array called 'with' that you can use, but it did not work for me. Somewhere else I read I should work with query scopes but I have no idea how that could help me.
Thanks for reading and your support!
Best regards, Marcel