国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Thinkphp5.1通過模型實現(xiàn)CURD操作

Original 2019-03-16 10:13:21 358
abstract:<?php namespace app\index\controller; use think\DB; use app\index\model\User; class Index {     public function index()     {
<?php
namespace app\index\controller;

use think\DB;
use app\index\model\User;
class Index
{
    public function index()
    {
    	
        return view();
    }

    public function find()
    {
    	// $res = User::find(); //用sql語句進(jìn)行查詢


    	// $res = User::where('user_id','>',6)->find(); //用模型進(jìn)行查詢一條數(shù)據(jù),返回最近的一條
    	$res = User::where(function($query){
    		$query->where('user_id','>',6);
    	})->find(); //用閉包函數(shù)進(jìn)行查詢條件創(chuàng)建
    	dump($res);
    }

    public function select()
    {
    	// $res = User::select();

    	$res = User::select(function($query){
    		$query->where('sallary','>',5000);
    	});
    	dump($res);
    }

    public function update()
    {
    	// $res = User::update(
    	// 	['sallary'=> DB::raw('sallary+1000')],
    	// 	['user_id'=>1]
    	// );
    	$res = User::update(
    		['sallary'=> DB::raw('sallary+1000')],
    		function($q){
    			$q->where('sallary','<','4000');
    		}
    	);
    	dump($res);
    }

    public function delete(){
    	// $res = User::destroy(1);//軟刪除成功

    	// $res = User::where('user_id','<',3)->select(); //獲取數(shù)據(jù),不包括軟刪除的數(shù)據(jù)
    	// $res = User::withTrashed()->where('user_id','<',3)->select(); //查詢所有數(shù)據(jù),包括軟刪除的數(shù)據(jù)
    	$res = User::onlyTrashed()->where('user_id','<',3)->select();//只獲取軟刪除的數(shù)據(jù)
    	dump($res);
    }

    public function insert()
    {
    	//$res = DB::query('select * from user');
    	// $data = ['user_name'=>'PHP','sex'=>1,'age'=>19,'sallary'=>8000];
    	// $res = DB::table('user')->insert($data); //插入一條數(shù)據(jù),返回影響的條數(shù)

    	$data = ['user_name'=>'bootstrap','sex'=>1,'age'=>52,'sallary'=>4300];

    	// $res = DB::table('user')->insertGetId($data);//插入一條數(shù)據(jù),并返回插入的ID
    	// $userModel = new User();
    	// $res = $userModel->save($data); //返回是個布爾型值

    	$res = User::create($data); //靜態(tài)方法進(jìn)行添加操作,返回是個對象類型
    	var_dump($res->user_id);die;
    }

    public function hello($name = 'ThinkPHP5',$sex='18')
    {
        return 'hello,' . $name.'年齡:'.$sex;
    }
}

模型代碼如下:

<?php

namespace app\index\model;

use think\Model;
use think\model\concern\SoftDelete;

class User extends Model
{
	use SoftDelete;
    
    protected $table = 'user'; //定義數(shù)據(jù)表

    protected $pk = 'user_id'; //定義主鍵,默認(rèn)為id

    protected $deleteTime = 'delete_time'; //設(shè)置軟刪除的字段

    protected $defaultSoftDelete = 0; //設(shè)置軟刪除默認(rèn)字段值


}

總結(jié):

  1. 閉包函數(shù):就是 把函數(shù)相當(dāng)于一個參數(shù)傳遞過去。

  2. 模型:模型可以設(shè)置很多條件,例如軟刪除,比原生sql語句使用起來方便。問:但是不知道速度差距明顯嗎,數(shù)據(jù)少顯示不出來,數(shù)據(jù)多了呢?

Correcting teacher:查無此人Correction time:2019-03-16 10:46:14
Teacher's summary:數(shù)據(jù)庫查詢的速度,在于你的條件。你的條件明確,對查詢速度不影響。繼續(xù)加油

Release Notes

Popular Entries