Cakephp - 将数据保存到关系模型

时间:2015-12-02 10:11:31

标签: php cakephp frameworks cakephp-3.0 cakephp-3.x

我有三个模型:usersgroupsuser_groups

users table: id | alias | password ...

groups table: id | name ...

user_groups table: id | user_id (fk users table) | group_id (fk groups table) ...

在我的群组控制器中,我收到有关可用群组和成员的数据:

public function edit($id)
{
    if (!$id) throw new NotFoundException(__('Unknown Group'));
    //All available groups with members
    $group = $this->Groups->find('all', [
        'contain' => 'Users'
    ]);
    //Only the group + members with the requested id
    $member = $this->Groups->get($id, [
        'contain' => 'Users'
    ]);

    if (!$group) throw new NotFoundException(__('Group does not exist'));
    if ($this->request->is(['post', 'put'])) {
        $this->Groups->patchEntity($group, $this->request->data);
        if ($this->Groups->save($group)) {
            $this->Flash->success(__('Group successfully updated'));
            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('Failed to update group'));
    }
    $this->set('group', $group);
    $this->set('member', $member);
}

我正在尝试构建一个包含两个selectboxes的视图,一个用于已经是该组成员的用户,另一个用于剩余(非成员)用户。

当涉及到关系模型(user_groups)的数据保存时,我完全无能为力。

我是否必须使用特定的变量名称/输入字段名称?

如何提供数据,以便控制器知道保存位置?

提前致谢并对那些noob问题感到抱歉,我对整个框架事情都很陌生。

0 个答案:

没有答案
相关问题