使用RedBean更新数据库中已存在的项目

时间:2015-11-13 17:53:39

标签: database sql-update redbean

我再次转向堆栈社区尝试解决我的问题。 我一起使用 Slim Twig RedBean 为我的网站创建了一个后端应用程序(长话短说,我'使用Slim可以将API和应用程序连接到它。)

(注意:我对RedBean来说相对较新,所以我还没有放心)

但是,我在尝试更新用户时尝试抓住了一个奇怪的异常错误。

以下几点有效: - 添加用户 - 删除用户

当我更新我的用户时,它会向我显示消息"此电子邮件已被使用"这是正常的,因为我是该帐号的电子邮件。但是,我无法通过错误......

这是异常文件(Models / Validation / Users.php):

class Users extends SimpleModel {

    public function update() {
        $firstname = trim($this->bean->firstname);
        $lastname = trim($this->bean->lastname);
        $email = trim($this->bean->email);

        if(isset($this->bean->firstname)){
            $firstname = trim($this->bean->firstname);
            if(empty($firstname)) {
                throw new ValidationException( 'You have to provide a firstname.' );
            }
        }

        if(isset($this->bean->lastname)){
            $lastname = trim($this->bean->lastname);
            if(empty($lastname)) {
                throw new ValidationException( 'You have to provide a lastname.' );
            }
        }

        if(isset($this->bean->email)){
            $email = trim($this->bean->email);
            if(empty($email)) {
                throw new ValidationException( 'You have to provide an email.' );
            }
        }

        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            throw new ValidationException( 'Not a valid email.' );
        }

        $searchUser = R::findOne( 'Users', ' email = ? ', [ $email ] );
        if(!empty($searchUser)){
            $equal = $this->bean->equals($searchUser);
        } else {
            $equal = FALSE;
        }

        if(!empty($searchUser) && !$equal) {
            throw new ValidationException( 'This email already exists.' );
        }   
    }
}

更新电话:

public function update( $id, $entry ) {
        try {
            $user = R::load( $this->config->getTable('users'), 'id = ? ', [ $id ] );
            if(array_key_exists('firstname',$entry)) $user->firstname = $entry['firstname'];
            if(array_key_exists('lastname',$entry)) $user->lastname = $entry['lastname'];
            if(array_key_exists('email',$entry)) $user->email = $entry['email'];

            (!empty($entry['password'])) ? $user->password = password_hash($entry['password'], PASSWORD_BCRYPT) : null;

            R::store($user);

        } catch(ValidationException $e) {
            $this->errors[] = $e->getMessage();
            return FALSE;
        }
    }

put控制器:

Class PutController extends Controllers\Controller {

    public function updateRecord() {

        $user = new Users($this->configManager('Database'));
        $this->data['user'] = $this->app->request->post();
        $password = trim($this->app->request->post('password'));
        $passwordConfirm = trim($this->app->request->post('password-confirm'));
        if((!empty($password) || !empty($passwordConfirm)) && ($password != $passwordConfirm)){
            $this->app->errors += ["Password and the confirmation are different"];
            return;
        }
        $user->update($this->args['id'], $this->app->request->post());
        $this->app->errors += $user->getErrors();

    }


}

最后路线:

$this->app->group('/update/:id', function () {

        // Display the page to update a user
        $this->app->map('/', function ($id) {
            $data = new Users\GetController( 'updateView', ['id' => $id] );
            $data->send("Users/update.template.html");
        })->via('GET');

        // Post request to update a user
        $this->app->map('/', function ($id) {
            $data = new Users\PutController( 'updateRecord', ['id' => $id] );
            if(!empty($this->app->errors)) {
                $this->app->flashNow("errors", $this->app->errors);
                $data->send("Users/update.template.html");
            } else {
                $this->app->flash("success", "Successfully updated the user.");
                $this->app->redirect('../');
            }
        })->via('POST', 'PUT');

    }); 

最后但并非最不重要的是,树枝形式:

<form action="" method="POST">
                  <div class="form-group">
                    <label for="firstname">Firstname</label>
                    <input value="{{user.firstname}}" type="text" class="form-control" id="firstname" name="firstname" placeholder="Firstname">
                  </div>
                  <div class="form-group">
                    <label for="lastname">Lastname</label>
                    <input value="{{user.lastname}}" type="text" class="form-control" id="lastname" name="lastname" placeholder="Lastname">
                  </div>
                  <div class="form-group">
                    <label for="email">Email address</label>
                    <input value="{{user.email}}" type="email" class="form-control" id="email" name="email" placeholder="Email">
                  </div>
                  <div class="form-group">
                    <label for="password">New password</label>
                    <input type="password" class="form-control" id="password" name="password" placeholder="Password">
                  </div>
                  <div class="form-group">
                    <label for="password-confirm">Confirm your password</label>
                    <input type="password" class="form-control" id="password-confirm" name="password-confirm" placeholder="Password">
                  </div>
                  <input type="submit" value="Submit" class="btn btn-default">
            </form>

我不知道如何克服这个错误。我尝试过添加冻结(true),但它没有用。

对于调试,我将其设置为true / false并且没有任何更改。

如果有人知道如何解决这个问题,我会非常感激!

谢谢!

1 个答案:

答案 0 :(得分:0)

我实际上用Davide的评论自己解决了我的问题,引发了一个想法。 这是一个愚蠢的错误:RedBean的equals()函数实际上检查了bean的类型,这就是问题所在。

我的一个bean有'Users'类型(有资本),另一个'用户'(没有资本)导致异常。

感谢Davide的火花! :)