Edit: My question may perhaps been overly complex, let's simplify. How do I import/call tables from different controllers and tables. E.g. how would I call UsersTable from ProjectsController?
If I want to access and handle data for a table that uses composite keys to tie two different tables together how should I do this?
As an example I have Projects and Users and a table called ProjectsUsers that keeps track of the relationship of the two (which users have been assigned to which project and their role in that project). Using composite keys I was easily able to bake scaffold code for add and edit that let's me assign users to projects when creating them, however this does not give me access to the role variable that is in the table.
I am thinking that I should make a model for the ProjectsUsers (which I did do using bake) and then make functions inside the ProjectsUsersTable to check wether a given user has access rights to whatever they are trying to access.
The ProjectsTable method isOwnedBy (below) is working fine and so I tried replicating it for my composite table but with no luck.
ProjectsController:
use App\Controller\AppController;
use App\Model\Table\ProjectsUsers;
class ProjectsController extends AppController
{
//Individual access rules to projects functions (projects/*).
public function isAuthorized($user)
{
// All registered users can add projects.
if ($this->request->action === 'add'){
return true;
}
// Check from the ProjectsUsers table if the person trying to access
// is a moderator of that project.
if ($this->request->action === 'edit'){
$projectId = (int)$this->request->params['pass'][0];
if ($this->ProjectsUsers->isModeratedBy($projectId, $user['id']))
{ return true;
}
}
// The owner of an article can edit and delete it.
if (in_array($this->request->action, ['edit', 'delete'])){
$projectId = (int)$this->request->params['pass'][0];
if ($this->Projects->isOwnedBy($projectId, $user['id'])){
return true;
}
}
return parent::isAuthorized($user);
}
}
ProjectsTable (working fine):
class ProjectsTable extends Table
{
public function isOwnedBy($projectId, $userId)
{
return $this->exists(['id' => $projectId, 'user_id' => $userId]);
}
}
ProjectsUsersTable:
class ProjectsUsersTable extends Table
{
public function isModeratedBy($projectId, $userId)
{
return true;
}
}
But I am getting an error:
Call to a member function isModeratedBy() on boolean
Edit: My question may perhaps been overly complex, let's simplify. How do I import/call tables from different controllers and tables. E.g. how would I call UsersTable from ProjectsController?