1、模型层
<?php
namespace app\common\model;
use think\Model;
use think\Db;
class Order extends Model
{
/**
* 查询
*/
public function find($id){
$data = Db::table('zt_order')->find($id);
return $data;
}
/**
* 删除
*/
public function del($id)
{
$data = Db::table('zt_order')->where('id', $id)->delete();
return $data;
}
/**
* 修改
*/
public function edit($id,$data){
$data = Db::table('zt_order')->where('id', $id)->update($data);
return $data;
}
/**
* 添加
*/
public function add($data){
$data = Db::table('zt_order')->insert($data);
return $data;
}
}
2、控制器层
<?php
namespace app\api\controller;
use think\Controller;
use app\common\model\Order as OrderModel;
class Order extends Controller
{
public function find($id=1){
$order = model('common/Order')->find($id);
if(!$order){
echo "调用model层错误。";
}
return json($order);
}
public function del($id=70140){
$order = model('common/Order')->del($id);
return json($order);
}
public function edit(){
$id=70133;
$data = [
'order_name' => '垃圾袋1'
];
$order = model('common/Order')->edit($id,$data);
return json($order);
}
public function add(){
$data = [
'order_no' => '123456',
'order_name' => '洗洁精1',
'order_status' => '1',
'add_time' => '1593424651',
'finance_remark' => '哈哈哈',
'freight_money' => '4',
'order_price' => '3.7'
];
$order = model('common/Order')->insert($data);
return json($order);
}
}