CakePHP 3显示学校所有的学院 [英] CakePHP 3 Display Colleges owned by Admins

查看:134
本文介绍了CakePHP 3显示学校所有的学院的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表学院和college_admins。每个学院由一个用户的CollegeAdmin拥有。我使用Auth组件来授权访问哪些用户可以查看哪些页面。

I have two tables colleges and college_admins. Each College is owned by a CollegeAdmin who is a User. I'm using the Auth component to authorize access to which pages can be viewed by which user.

现在,我想让每个CollegeAdmin只列出他/

Now, I want to each CollegeAdmin to list only Colleges owned by him / her.

这是我在CollegesController中执行的操作:

This is what I'm doing in the CollegesController:

public function index()
{
    $this->set('colleges', $this->paginate($this->Colleges->ownedBy($this->Auth->user('id'))));
    $this->set('_serialize', ['colleges']);
}

这是我在CollegesTable中使用的代码:

And this is the code I'm using in the 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]);
}

但它不工作。它正确显示行/记录的数量,但字段显示为空白。如何解决这个问题?

But it's not working. It's showing the number of rows / records correctly, but the fields are being displayed as blank. How to fix this?

推荐答案

我找到了问题的解决方案。我在学院表中做错了路。此代码完美地工作:

I found the solution to the problem. I was doing it the wrong way round in the Colleges table. This code works perfectly:

public function ownedBy($userId)
{
    return $this->find()
            ->leftJoinWith('CollegeAdmins')
            ->where(['CollegeAdmins.user_id' => $userId]);
}

这篇关于CakePHP 3显示学校所有的学院的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆