Blogger Information
Blog 5
fans 0
comment 0
visits 2216
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
模型基本操作—2018年5月24日12时28分
候磊的博客
Original
433 people have browsed it
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;
    
}


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post