本文整理匯總了PHP中UserRole::transaction方法的典型用法代碼示例。如果您正苦於以下問題:PHP UserRole::transaction方法的具體用法?PHP UserRole::transaction怎麽用?PHP UserRole::transaction使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類UserRole
的用法示例。
在下文中一共展示了UserRole::transaction方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: update
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' => '更新成功!'));
}