我再次轉向堆棧社區嘗試解決我的問題。 我使用修身,嫩枝和紅豆共同打造我的網站後端的應用程序(長話短說,我使用的修身,以能夠連接的API和應用程序的話)。使用RedBean更新數據庫中已有的項目
(注:我是比較新的紅豆,所以我沒有在用它輕鬆)
不過,我從一個try和catch試圖更新用戶時得到一個奇怪的異常錯誤。
以下幾點但工作: - 添加用戶 - 刪除用戶
當我更新我的用戶,它給我的消息「此郵件已被使用」,這是正常的,因爲我是它的帳戶的電子郵件。但是,我不能得到通過的錯誤...
這裏是例外文件(型號/確認/ 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;
}
}
認沽控制器:
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>
我不知道如何讓過去的這個錯誤。我嘗試添加凍結(真),但它沒有奏效。
同樣的事情調試,我把它設置爲真/假,沒有任何改變。
如果任何人有關於如何解決這個問題的提示,我會非常感激!
謝謝!
更好的解決方案是檢查新的電子郵件地址是否與用戶(給定的id)相同。如果沒有,它已經在那裏,你應該顯示錯誤。 –
你好大衛,謝謝你的回答!其實我試圖在RedBean中使用「等號」。但是,多虧你,我實際上回到了這個功能的工作方式。有什麼不對的是,搜索的bean的類型和新的bean(更新的)是不一樣的:現有類型是'用戶'(大寫)和新用戶''。現在已經解決了這個愚蠢的錯誤。無論如何感謝您的回答! – blackjak231