博客列表 >模型基本操作—2018年5月24日12时28分

模型基本操作—2018年5月24日12时28分

候磊的博客
候磊的博客原创
2018年05月29日 17:34:49421浏览
Staff.php(controller)
<?php 
namespace app\index\controller;
use think\Controller;
use app\index\model\Staff as StaffModel; 
class Staff extends Controller
{
      //模型闭包查询
	public function query()
	{
		$staff = StaffModel::get(function($query){
			$query->where('sex',0)->where('salary','<',4000)->order('salary','desc');
		});
		echo '性别为男,工资小于4000的员工信息:<br>';
		dump($staff);
		
	
		$age = $this->request->param('age') ?: 40;
		$salary = $this->request->param('salary') ?: 5000;

		$staffs = StaffModel::all(function($query) use ($age, $salary){
			$query->where('age','<=',$age)->where('salary','>',$salary);
		});
	
		foreach ($staffs as $staff) {
			echo '姓名:'.$staff->name.'<br>';
			echo '年龄:'.$staff->age.'<br>';
			echo '工资:'.$staff->salary.'<hr>';
		}
	}


	//模型软删除
	public function softDelete()
	{
		StaffModel::destroy(1);
		$res = StaffModel::all();
		dump($res);
	}
}

Staff.php(model)
<?php
namespace app\index\model;
use think\Model;
use think\model\concern\SoftDelete;

class Staff extends Model
{
   use SoftDelete;
    protected $table = 'staff';
    protected $pk = 'staff_id';
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = 0;
    
}


声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议