返回基于模型 进行......登陆

基于模型 进行增删改查操作(物理删除和软删除都要有)

肖凌2019-10-29 16:34:0027

//model扩展类

<?php

         namespace app\model;

         use think\Model;

         use think\model\concern\SoftDelete;

         class User extends Model{

                     use SoftDelete;

                     protected $pk = 'id';

                     protected $table = 'reader';

                     protected $deleteTime = 'delete_time';

                     protected $defaultSoftDelete = 0;

          }

//基于模型控制器方法 User.php

<?php

         namespace app\index\controller;

        use think\Controller;

        use app\model\User as modelUser;

        class User extends Controller{

                 //单条查询

                 public function sele(modelUser $user){

                $res = $user::get(2);

                dump($res);

        }

         //多条查询

         public function select(modelUser $user){

                  $res = $user::all(function($query){

                 $query->where('salary','>','3000')

                           ->where('age','>','32');

                 });

                dump($res);

        }

        //插入单条数据

         public function insert(modelUser $user){

                    $data = [

                             'name' => 'alice',

                             'age'  => 28,

                             'tel'  => '18956378989',

                             'email'=> '18956378989@139.com',

                             'salary'=> '5600'

                     ];

                     $user::create($data);

         }

             //插入多条记录

             public function inserAll(modelUser $user){

                        $data = [

                                     ['name' => 'alice','age'  => 28,'tel'  => '18956378989','email'=> '18956378989@139.com','salary'=> '5600'],

                                     ['name' => '陈大益','age'  => 36,'tel'  => '15986379696','email'=> '15986379696@139.com','salary'=> '3900'],

                                     ['name' => '许大上','age'  => 42,'tel'  => '15236897979','email'=> '15236897979@qq.com','salary'=> '6500']

                        ];

                        $res = $user->insertAll($data);

                        echo $res ? '插入成功' : '插入失败';

             }

             //更新操作

             public function update(modelUser $user){

                        $user::update(['salary' => \think\Db::raw('salary+500')],function($query){

                                                $query->where('salary < 4000');

                                            });

                        echo '更新成功';

            }

         //物理删除

         public function delete(modelUser $user){

                    //$res = $user::destroy(4);

                     $res = $user::destroy(function($query){

                                        $query->where('salary > 6000');

                        });

                       echo $res ? '删除成功' : '删除失败';

         }

             //软删除

             public function softdel(modelUser $user){

                                    $res = $user::destroy(6);

                                 //普通查询看不到时间戳字段的记录

                                 //dump($user::all());

                                 //查询回收站的记录

                                 //dump($user::onlyTrashed()->all());

                                 //查询含回收站的全部记录

                                 dump($user::withTrashed()->all());

            }

    }


最新手记推荐

• 依赖注入小例子• 模板显示用户信息表,并分页显示• 模板继承,模仿php页面• 模拟用户登录验证的案例 依赖注入 Facade• 创建一个验证器, 用来对模型对应的表中字段进行验证处理

全部回复(0)我要回复

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