返回 模型的增删改查...... 登陆

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

Levin 2018-11-27 14:18:11 286

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

<?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);

    }

}


最新手记推荐

• 用composer安装thinkphp框架的步骤 • 省市区接口说明 • 用thinkphp,后台新增栏目 • 管理员添加编辑删除 • 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消 回复 发送
  • PHP中文网