ThinkPHP3.2.3对数据的添、删、改、查(CURD)

时间:2022-06-17 20:15:32

对数据的添加:

public function form()
{
parent
::common();

$obj = D('Leave');
if (IS_POST) {
$data = I('post.');
if ($obj->create($data)) {
$rs = $obj->add();
if ($rs) {
//$this->success('新增成功', 'Approval/approval');
$this->redirect('Approval/approval');
}
}
else {
$this->assign('error', json_encode($obj->getError()));
$error1 = $obj->getError();
$this->assign('error1', $error1);
}
}

对数据的修改:

 $data = I('post.');

D('Leave')->Approver = $data['Approver'];

D('Leave')->where("id={$data['id']}")->save(); 

对数据的查询:

//控制器中关联查询(leave表与user表关联)

D(
'Leave')->relation(true)->where('Leave.depart_id=2and Leave.job_id!=5')->select();

//leave模型中

protected $_link = [
'user' => self::BELONGS_TO
];