做软删除的时候出现的sql语句不是更新语句

原创 2018-12-16 21:23:25 249
摘要:<?php  namespace app\index\controller; use think\Controller; use app\index\model\Score as ScoreModel; //设置别名为ScoreModel class Score extends Control
<?php 
namespace app\index\controller;
use think\Controller;
use app\index\model\Score as ScoreModel; //设置别名为ScoreModel
class Score extends Controller
{
	public function instance(ScoreModel $score)
	{
		// $score = new ScoreModel();
		// dump($score->getName());
		// 新增一条记录
		$score->name = '艾米丽';
		$score->sex = '1';
		$score->math = '80';
		$score->java = '70';
		$score->php = '60';
		$score->save();
		return '新增一条记录id='.$score->id;
	}

	//模型查询
	public function query()
	{
		//单条记录查询
		$score = ScoreModel::get(2); //以静态模式创建模型对象
		dump($score);
		\var_dump($score); //这个是能看到原始数据类型:object
		echo $score['name'],'<br>';   //数组形式
		echo $score->math,'<br>';     //对象形式

		//查询条件复杂时 可以使用闭包的形式。
		$score = ScoreModel::get(function ($query){
			$query->where('sex',0)->where('math','>',90);
		});
		echo "性别为男且数学分数大于90的信息:<br>";
		dump($score);
	    echo "<hr>";

	    //多条记录查询 all
	    $score = ScoreModel::all();
	    dump($score);
	    echo "<hr>";
	    $score = ScoreModel::all([1,3,5]); //查询主键为1 3 5的记录
	    dump($score);
	    echo "<hr>";
	    //all()页支持闭包查询 以后会经常用!!!
	    $score = ScoreModel::all(function($query){
	    	$query->where('math','<=',90)->where('sex','=',0);
	    });
	    dump($score);
	    echo "<hr>";

	}


	//模型更新
	public function update()
	{
		$score = ScoreModel::get(1);
		$score->name ='谢耳朵';
		$score->save();
		//强烈推荐用下面的静态方法来更新数据
		ScoreModel::update(
			['name'=>'谢尔顿'],
			['id'=>1]
		);

		//复杂的更新操作
		// 将数学分数大于90分的人,java分加10
		ScoreModel::update(
			['java' => \think\Db::raw('java+10')],
			function($query){
				$query->where('math','>',50);
			}
		);
	}



	//模型创建 添加数据
	public function create()
	{
		$data = [
			'name' => '斯图尔特',
			'sex' => 0,
			'math' => 77,
			'java' => 66,
			'php' => 55
		];
		//设置允许添加的字段名,不在列表的字段 有值也不添加 建议在表中创建默认值
		$field = ['name','sex','math','java','php'];
		ScoreModel::create($data,$field);
	}


	//模型删除 删除记录
	public function delete()
	{
		//删除采用静态方法destory(主键/闭包)
		//ScoreModel::destroy(13);
		//也可以多主键删除	ScoreModel::destroy([1,5,10,11]);
		//删除条件推荐使用闭包查询
		//删除数学小于等于50 java小于等于60的记录
		ScoreModel::destroy(function($query){
			$query->where('math','<=',50)->where('java','<=',60);
		});
	}



	//软删除 :必须在模型中进行先行配置
	public function softDelete()
	{
		ScoreModel::destroy(6);
		//我的执行完 sql语句是 SELECT * FROM `score` WHERE `id` = 6
	}
	
}
?>





<?php
//model目录下的文件
namespace app\index\model;

use think\Model;
//使用软删除功能,必须先导入model/concern/SoftDelete.php
use think\model\concern\SoftDelete;    //实际上一个trait方法集
class Score extends Model
{
	use SoftDelete;

    //设置数据表名
    protected $table = 'score';

    //设置主键
    protected $pk = 'id';

    //设置删除时间字段,配合软删除功能
    protected $deleteTime = 'delete_time';

    //设置软删除字段的默认值
    protected $defaultSoftDelete = 0;
    
}
?>

我在做软删除的时候 配置好后执行代码 出来的不是更新语句,而是SELECT * FROM `score` WHERE `id` = 6 。

批改老师:韦小宝批改时间:2018-12-17 09:18:52
老师总结:写的是没有啥问题啊!你和老师的源码对照着排查一下看看!

发布手记

热门词条