加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – Laravel / Ardent /用户模型编辑保存

发布时间:2021-08-28 03:46:24 所属栏目:大数据 来源: https://www.jb51.cc
导读:在laravel / ardent中用密码编辑用户模型的预期方法是什么?我的问题是,在正确验证用户输入之前,我不想从db加载实际的用户模型.当我将密码字段留空时验证显然失败,因为需要密码.这是我目前的编辑后动作: public function postEdit($id){ // ardent autohydr

在laravel / ardent中用密码编辑用户模型的预期方法是什么?我的问题是,在正确验证用户输入之前,我不想从db加载实际的用户模型.当我将密码字段留空时验证显然失败,因为需要密码.这是我目前的编辑后动作:

public function postEdit($id)
{
    // ardent autohydrates this model
    $newUser = new User;

    // validation fails
    if(!$newUser->validate())
        return Redirect::action('UsersController@getEdit', $id)
            ->with('error', Lang::get('Bitte Eingabe überprüfen'))
            ->withErrors($newUser->errors())
            ->withInput(Input::except('password'));

    // load model from db
    $exUser = User::find($id);
    if(!$exUser->exists)
        return Response::make('No such user', 500);

    // save model, ardent autohydrates again?
    if($exUser->save())
        return Redirect::action('UsersController@getShow', $id)
            ->with('success', Lang::get('?nderungen gespeichert'));
    else
        return Redirect::action('UsersController@getEdit', $id)
            ->with('error', Lang::get('Bitte Eingabe überprüfen'))
            ->withErrors($newUser->errors())
            ->withInput(Input::except('password'));
}

这似乎是一个非常多的代码(它不工作),我无法找到这种情况的例子

解决方法:

好的,我自己解决了,因为这不是一个非常活跃的话题.

问题是结合了ardents自动水合功能和保留旧密码的独特要求,如果没有给出新密码.因为在validate()和save()上进行了自动水合,所以也无法阻止自动水合空密码.首先,我尝试更改Input数组并使用旧密码覆盖它,但之后我只是关闭了用户模型的自动水合:

class User extends Ardent implements UserInterface, RemindableInterface {

    public $forceEntityHydrationFromInput = false;
    public $autoHydrateEntityFromInput = false;

这是POST上的编辑操作:

public function postEdit($id)
{
    // manually insert the input
    $user = new User(Input::all());

    // validate the user with special rules (password not required)
    if($user->validate(User::$updateRules)) {

        // get user from database and fill with input except password
        $user = User::find($id);
        $user->fill(Input::except('password'));

        // fill in password if it is not empty
        // will flag the pw as dirty, which will trigger rehashing on save()
        if(!empty(Input::get('password')))
            $user->password = Input::get('password');

        if($user->save())
            return Redirect::action('UsersController@getIndex')
                ->with('success', Lang::get('?nderungen gespeichert'));
    }

    return Redirect::action('UsersController@getEdit', $id)
        ->with('error', Lang::get('Bitte Eingaben überprüfen'))
        ->withErrors($user->errors())
        ->withInput(Input::except('password'));
}

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读