模型的增删改查,(上次老师跟我说要高亮。我不知道怎么高亮啊)

原创 2018-11-27 14:18:11 355
摘要:这次练习主要是模型的增删改查,教程中教了几种方法,我觉得应该要把其中一种学熟,而老师推荐用闭包的方法,所以这次练习,主要是闭包为主。另外也试了一下各种参数<?phpnamespace app\index\controller;use think\Controller;use app\index\model\cate as cateM;use think\Db;use think\reques

这次练习主要是模型的增删改查,教程中教了几种方法,我觉得应该要把其中一种学熟,而老师推荐用闭包的方法,所以这次练习,主要是闭包为主。另外也试了一下各种参数

<?php

namespace app\index\controller;

use think\Controller;

use app\index\model\cate as cateM;

use think\Db;

use think\request;

class Cate extends Controller

{

    //闭包查询

    public function select()

    {

        //单调查询

        $cate = cateM::get(function($suibian){

            $suibian->field(['catename','pid'])->where('type',2)

            ->where('rec_index',1);

        });

        dump($cate);

        //多条查询

        $type = $this->request->param('type')?:5;

        $cate2 = cateM::all(function($suibian)use($type){

            $suibian->where(['type'=>$type]);

        });

        dump($cate2);

    }


    public function update()

    {

        //使用闭包更新

        $cate = cateM::update(['type'=>5,'rec_index'=>3],function($suibian){

            $suibian->where('type','<',3);

        },'type');


        //使用闭包数据加减

          $cate = cateM::update(['type'=>Db::raw('type+50'),'rec_index'=>3],function($suibian){

            $suibian->where('type','>',6);

        },'type');

    }


    public function add()

    {

        $data=[

            'catename'=>'山地单车',

            'type'=>'2',

            'keywords'=>'爬坡'

        ];

        $field = ['type','catename'];


        $cate = cateM::create($data,$field);

    }


    public function delete()

    {

        $cate = cateM::destroy(function($suibian){

            $suibian->where('type',3);

        });

    }


    public function softDelete()

    {

        cateM::destroy(49);

    }

}


批改老师:查无此人批改时间:2018-11-27 15:16:23
老师总结:写的不错,是哪位老师说的高亮,你可以私聊他。不懂的一定要问。加油

发布手记

热门词条