我有两个表学院和college_admins。每个学院都由作为用户的CollegeAdmin拥有。我使用Auth组件授权访问哪些用户可以查看哪些页面。
现在,我希望每个CollegeAdmin只列出他/她所拥有的学院。
这就是我在CollegesController中所做的:
public function index()
{
$this->set('colleges', $this->paginate($this->Colleges->ownedBy($this->Auth->user('id'))));
$this->set('_serialize', ['colleges']);
}
这是我在CollegesTable中使用的代码:
public function ownedBy($userId)
{
$college_admins = TableRegistry::get('CollegeAdmins');
return $college_admins->find()
->select($college_admins->Colleges)
->leftJoinWith('Colleges')
->where(['CollegeAdmins.user_id' => $userId]);
}
但它没有用。它正确显示行数/记录数,但字段显示为空白。如何解决这个问题?
答案 0 :(得分:1)
我找到了问题的解决方案。我在学院餐桌上做错了。这段代码完美无缺:
public function ownedBy($userId)
{
return $this->find()
->leftJoinWith('CollegeAdmins')
->where(['CollegeAdmins.user_id' => $userId]);
}