1

我有三个模型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) 时,我完全无能为力。

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

如何提供数据以便控制器知道将其保存在哪里?

在此先感谢并为那些菜鸟问题感到抱歉,我对整个框架都是新手。

4

0 回答 0