public function update() { if (!$this->has_post()) { return redirect_message(array('admin', $this->get_class(), $this->user->id, 'edit'), array('_flash_message' => '非 POST 方法,錯誤的頁面請求。')); } $posts = OAInput::post(); if ($msg = $this->_validation_posts($posts)) { return redirect_message(array('admin', $this->get_class(), $this->user->id, 'edit'), array('_flash_message' => $msg, 'posts' => $posts)); } if ($columns = array_intersect_key($posts, $this->user->table()->columns)) { foreach ($columns as $column => $value) { $this->user->{$column} = $value; } } $user = $this->user; $update = user::transaction(function () use($user) { return $user->save(); }); if (!$update) { return redirect_message(array('admin', $this->get_class(), $this->user->id, 'edit'), array('_flash_message' => '更新失敗!', 'posts' => $posts)); } $ori_keys = column_array($user->roles, 'name'); if (($del_keys = array_diff($ori_keys, $posts['roles'])) && ($roles = UserRole::find('all', array('select' => 'id', 'conditions' => array('user_id = ? AND name IN (?)', $user->id, $del_keys))))) { foreach ($roles as $role) { UserRole::transaction(function () use($role) { return $role->destroy(); }); } } if ($add_keys = array_diff($posts['roles'], $ori_keys)) { foreach ($add_keys as $add_key) { UserRole::transaction(function () use($add_key, $user) { return verifyCreateOrm(UserRole::create(Array_intersect_key(array('name' => $add_key, 'user_id' => $user->id), UserRole::table()->columns))); }); } } return redirect_message(array('admin', $this->get_class()), array('_flash_message' => '更新成功!')); }