修改數(shù)據(jù)跟添加數(shù)據(jù)類似,其實(shí)是完全寫在一起的,為了更能清晰的知道流程,我這里還是分開寫
接著增加的數(shù)據(jù)進(jìn)行修改操作
在控制器里面添加edit操作
<?php namespace app\frontend\controller; use \tpfcore\Core; class User extends FrontendBase { public function add() { IS_POST && $this->jump(Core::loadModel($this->name)->saveUser($this->param)); return $this->fetch("add"); } public function edit(){ IS_POST && $this->jump(Core::loadModel($this->name)->editUser($this->param)); return $this->fetch("edit",[ "list"=>Core::loadModel($this->name)->listUser($this->param) ]); } }
這里跟添加差不多,主要是查詢要修改的那句,為了展示我要修改的是哪條語句,當(dāng)然你要傳遞要修改的數(shù)據(jù)主鍵跟對應(yīng)的值過來,例如id=1
<?php // +---------------------------------------------------------------------- // | Author: yaoyihong <510974211@qq.com> // +---------------------------------------------------------------------- namespace app\frontend\service;
use app\common\service\ServiceBase;
use \tpfcore\Core;
/**
* 基礎(chǔ)服務(wù)
*/
class User extends FrontendBase
{
public function editUser($data){
// 在進(jìn)行數(shù)據(jù)操作前進(jìn)行數(shù)據(jù)驗(yàn)證
$validate=\think\Loader::validate($this->name);
$validate_result = $validate->scene('edit')->check($data);
if (!$validate_result) {
return [RESULT_ERROR, $validate->getError(), null];
}
return Core::loadModel($this->name)->saveObject($data);
}
public function listUser($data){
return Core::loadModel($this->name)->listUser($data);
}
}
<?php namespace app\common\logic; use \tpfcore\Core; class User extends LogicBase { public function saveUser($data){ $result=self::saveObject($data); if($result){ return [RESULT_SUCCESS,"操作成功"]; } return [RESULT_ERROR,"操作失敗"]; } / 獲取用戶 / public function listUser($data){
return self::getList($data);
}
}
省略edit場景驗(yàn)證.....
視圖層拿來要修改的數(shù)據(jù)填充
<!DOCTYPE html> <html> <head> <title>添加用戶</title> </head> <body> <form method="post" action="{:url('User/edit')}"> <input type="hidden" name="id" value="{$list[0]->id}" /> <input type="text" name="username" value="{$list[0]->username}" /><br/> <input type="password" name="password" value="{$list[0]->password}" /><br/> <input type="submit" value="操作操作"/> </form> </body> </html>
這樣就完成了一個(gè)修改的操作了
更多建議: