在codeigniter ion auth中更新组

时间:2016-05-11 05:54:21

标签: php codeigniter ion-auth

我是codeigniter ion auth的新手并将其应用于项目中。遵循documentation,一切正常。在探索时,我注意到当我更新/编辑任何用户数据时,它不会抛出任何错误,但users_groups没有更新。

代码:

$id = $this->input->post('id');

$data = array(
  'first_name' => $this->input->post('first_name'),
  'last_name' => $this->input->post('last_name'),
  'group' => array($this->input->post('group_id')),
  'active' => $this->input->post('active')
  );

$result = $this->ion_auth->update($id, $data);

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:2)

使用ion_auth->update方法,您只能更新存储在users表中的用户属性,您无法修改用户组。
(在内部,它从users表中查询列并仅更新那些作为用户有效属性的参数) 要修改用户组,您需要执行以下操作:

$user_id = 123;
$group_id = 3;// let's say 3 is the ID of the 'publisher' user group

// to remove the user (#ID:123) from the 'publisher' group call this:
$this->ion_auth->remove_from_group($group_id, $user_id);
// to remove the user (#ID:123) from all of the assigned groups call this:
$this->ion_auth->remove_from_group(false, $user_id);

// to add the user (#ID:123) to the 'publisher' group call this:
$this->ion_auth->add_to_group($group_id, $user_id);
相关问题