【问题标题】:Accessing composite table data in cake php在 cake php 中访问复合表数据
【发布时间】:2016-02-07 09:38:28
【问题描述】:

编辑:我的问题可能过于复杂,让我们简化一下。如何从不同的控制器和表中导入/调用表。例如。如何从 ProjectsController 调用 UsersTable?

如果我想访问和处理使用复合键将两个不同表绑定在一起的表的数据,我应该怎么做?

例如,我有 Projects 和 Users 以及一个名为 ProjectsUsers 的表,用于跟踪两者之间的关系(哪些用户已被分配到哪个项目以及他们在该项目中的角色)。使用复合键,我可以轻松地为添加和编辑烘焙脚手架代码,让我在创建项目时将用户分配给项目,但这并不能让我访问表中的角色变量。

我在想我应该为 ProjectsUsers 制作一个模型(我确实使用了 bake),然后在 ProjectsUsersTable 中制作函数来检查给定用户是否有权访问他们尝试访问的任何内容。

ProjectsTable 方法 isOwnedBy(如下)运行良好,因此我尝试为我的复合表复制它,但没有成功。

项目控制器:

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(工作正常):

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;
    }

}

但我收到一个错误:

在布尔值上调用成员函数 isModeratedBy()

编辑:我的问题可能过于复杂,让我们简化一下。如何从不同的控制器和表中导入/调用表。例如。如何从 ProjectsController 调用 UsersTable?

【问题讨论】:

标签: php mysql cakephp composite-primary-key


【解决方案1】:

我以为我已经尝试过了,所以我困惑了一段时间,但正确的做法是:

use Cake\ORM\TableRegistry;

$articles = TableRegistry::get('Articles');

【讨论】:

    【解决方案2】:

    您应该考虑在模型中使用 CakePHP 关联:

    项目表:

    class ProjectsTable extends Table
    {
        public function initialize(array $config)
        {
            $this->belongsToMany('Users', [
                'through' => 'ProjectUsers',
            ]);
        }
       ...
    }
    

    用户表:

    class UsersTable extends Table
    {
        public function initialize(array $config)
        {
            $this->belongsToMany('Projects', [
                'through' => 'ProjectUsers',
            ]);
        }
        ...
    }
    

    ProjectsUsersTable:

    class ProjectsUsersTable extends Table
    {
        public function initialize(array $config)
        {
            $this->belongsTo('Projects');
            $this->belongsTo('Users');
        }
        ....
    }
    

    如果您遵循 CakePHP 关联,您可以使用可包含行为进行查询。如果您使用的是 ProjectsController:

    //Find all projects and it's related users.
    $projects = $this->Projects->find('all')->contain(['Users']);
    
    //Find all users and it's related projects.
    $this->loadModel('Users');
    $users = $this->Users->find('all')->contain(['Projects']);
    
    //Find certain user and it's related projects.
    $this->loadModel('Users');
    $user = $this->Users->find('all')->contain(['Projects'])->where(['Users.id' => 1]);
    //After this you can access the projects assigned to this user through $user->projects
    

    注意:还有其他方法可以解决这个问题,但我会这样做。

    【讨论】:

    • 我的表有默认的烘焙关联,但我现在遇到了项目和 project_tickets 的内部连接问题。 projects_tickets 设置为属于 jointype 为“inner”的项目,projects 设置为属于多个带有“projects_tickets”可连接的票,但我仍然收到错误消息:“Projects is not associated with ProjectsTickets”,你有什么见解吗这个?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    相关资源
    最近更新 更多