How do you handle this situation:
- You have a user that work in a task
- Task is an model in your project
- You have another model with relation to Task: TaskPermission that has task_id and user_id
Of course you can always query manualy database (detailed steps below - sorry for any typo! I can't validate code right now):
$task = Task::findFirstById($id);
foreach($task->getTaskPermission() as $permission){
$allow = ($this->session->auth->user_id == $permission->getUserId())?true:false;
}
Or extend a method
public static function findFirstById($id){
$task = parent::findFirstById($id);
$permission = Permission::find([
'task_id = :tid: AND user_id = :uid:',[
'tid' => $task->getId(),
'uid' => DI::get('session')->auth->user_id
]);
if($permission) {
return $task;
}else{
return false; //or throw an exception
}
}
There are a thousand of different approaches. What is yours? Anyone using behavior? I am really curious about it.