第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定

軟刪除:

首先在模型里面引用SoftDelete
<?php
namespace?app\index\model;
use?think\Model;
use?traits\model\SoftDelete;
class?User?extends?Model{
????use?SoftDelete;
????#autowritetimestamp表示創(chuàng)建與更新的時間戳都被打開
????protected?$autoWriteTimestamp?=?true;
????#刪除數(shù)據(jù)的時候刪除時間戳默認(rèn)寫入字段delete_time中,當(dāng)要自定義時:
//????protected?$deleteTime?=?'自定義刪除時間字段名';
}

然后在控制器里面執(zhí)行操作

<?php
namespace?app\index\controller;
use?think\Controller;
use?app\index\model\User;
class?Index?extends?Controller
{
????public?function?index(){
//????????$res?=?User::destroy(4);//被軟刪除
//????????$res?=?User::get(2);//返回NULL

????????#查詢包含已刪除的數(shù)據(jù)
????????//$res?=?User::withTrashed(true)->find(2);

????????#查詢僅包含已刪除的數(shù)據(jù)
????????$res?=?User::onlyTrashed()->select();
????????foreach?($res?as?$val){
????????????dump($val);
????????}
????????#若要恢復(fù)被軟刪除的數(shù)據(jù),直接用update方式將delete_time的值設(shè)置為NULL即可

????????#當(dāng)開啟軟刪除后要想真正徹底刪除數(shù)據(jù),在destroy的第二個參數(shù)后面?zhèn)魅胍粋€true值
????????$res?=?User::destroy(1,true);

????????#通過get方式進(jìn)行軟刪除/刪除
????????$res?=?User::get(3);//如果此處數(shù)據(jù)已經(jīng)被軟刪除則獲取到的為NULL,后面的操作無效
????????$user->delete();//軟刪除
????????$res?=?$user->delete(true);//刪除
????}
}


1
采集 1

模型時間戳:

<?php
namespace?app\index\model;
use?think\Model;

class?User?extends?Model{
????#autowritetimestamp表示創(chuàng)建與更新的時間戳都被打開
????protected?$autoWriteTimestamp?=?true;
//????#createtime為false表示創(chuàng)建的時間戳被關(guān)閉
//????protected?$createTime?=?false;
//????#updatetime為true表示更新時間戳被打開
//????protected?$updateTime?=?true;
//????#創(chuàng)建數(shù)據(jù)的時候創(chuàng)建時間戳默認(rèn)寫入字段create_time中,當(dāng)要自定義時:
//????protected?$createTime?=?'自定義創(chuàng)建時間字段名';
//????#更新數(shù)據(jù)的時候更新時間戳默認(rèn)寫入字段update_time中,當(dāng)要自定義時:
//????protected?$updateTime?=?'自定義更新時間字段名';
}


1
采集 1

模型修改器與自動完成

<?php
namespace?app\index\model;
use?think\Model;
class?User?extends?Model{
????#get+字段名+Attr
????public?function?getGenderAttr($val){
????????switch?($val){
????????????case?"1";
????????????????return?'男';
?????????????????break;
????????????case?"2";
?????????????????return?'女';
??????????????????break;
????????????default;
?????????????????return?'未知';
?????????????????break;
????????}
????}
????#模型修改
//????public?function?setPasswordAttr($val,$data){
//????????return?$val.$data['email'];
//????}

????#無論對數(shù)據(jù)庫執(zhí)行插入&更新操作,總是在數(shù)據(jù)中加上time字段相應(yīng)的值
????protected?$auto?=?[
??????'time'//字段名
????];
????public?function?setTimeAttr(){
????????return?time();//字段對應(yīng)的值
????}

????#insert/update對數(shù)據(jù)庫執(zhí)行插入/更新操作時,在數(shù)據(jù)字段中加入對應(yīng)的值
????protected?$insert?=?[
????????'insert'//字段名
????];
????public?function?setInsertAttr(){
????????return?time();
????}
}


0
采集 0

3-7 模型獲取器

2018-05-08

模型獲取器:

首先設(shè)置User模型:

<?php
namespace?app\index\model;
use?think\Model;
class?User?extends?Model{
????#get+字段名+Attr
????public?function?getGenderAttr($val){
????????switch?($val){
????????????case?"1";
????????????????return?'男';
?????????????????break;
????????????case?"2";
?????????????????return?'女';
??????????????????break;
????????????default;
?????????????????return?'未知';
?????????????????break;
????????}
????}
}

然后在index控制器中操作:

<?php
namespace?app\index\controller;
use?think\Controller;
use?app\index\model\User;
class?Index?extends?Controller
{
????public?function?index(){
????????$res?=?User::get(1);
????????dump($res->gender);//獲取性別單欄數(shù)據(jù)
????????dump($res->toArray());//以數(shù)組形式獲取整條數(shù)據(jù),性別顯示為男/女/未知
????????dump($res->getData());//以原始形式獲取整條數(shù)據(jù),性別顯示為1/2/其他
????}
}


0
采集 0

模型聚合操作:

count/sum/max/min/avg,用法均一樣

<?php
namespace?app\index\controller;
use?think\Controller;
use?app\index\model\User;
class?Index?extends?Controller
{
????public?function?index(){
????????#count獲取表中數(shù)據(jù)的條數(shù)
//????????????$res?=?User::count();
????????#給count加where條件
//????????$res?=?User::where('id','>',5)
//????????????->where('id','<',10)
//????????????->count();

????????#max/min獲取指定字段中數(shù)據(jù)的最大/小值
//????????$res?=?User::max('id');
????????#給max/min添加where條件
//????????$res?=?User::where('id',"<",9)->count('id');

????????#sum對指定字段數(shù)據(jù)求和
//????????$res?=?User::sum('id');
????????#對sum添加where條件
//????????$res?=?User::where('id','<',8)->sum('id');

????????#avg對指定字段數(shù)據(jù)求平均值
//????????$res?=?User::avg('id');
????????#對avg添加where條件
????????$res?=?User::where('id','<',8)->avg('id');
????????????dump($res);
????}
}


0
采集 0

使用MODEL對象對數(shù)據(jù)進(jìn)行刪除:

<?php
namespace?app\index\controller;
use?think\Controller;
use?app\index\model\User;
class?Index?extends?Controller
{
????public?function?index(){
????????#destroy返回影響行數(shù)
//????????$res?=?User::destroy(function?($query){
//???????????$query->where('id','<',5)
//???????????????->whereOr('id','>',10)?;
//????????});
//????????dump($res);

????????#get返回影響行數(shù)
//????????$userModel?=?User::get(7);
//????????$res?=?$userModel->delete();
//????????dump($res);

????????#delete,返回影響行數(shù)
????????$res?=?User::where('id',10)->delete();
????????dump($res);
????}
}


0
采集 1

使用模型更新表數(shù)據(jù):

<?php
namespace?app\index\controller;
use?think\Controller;
use?app\index\model\User;
class?Index?extends?Controller
{
????public?function?index(){
????????#當(dāng)數(shù)據(jù)中存在自增id時,數(shù)據(jù)更新方式,返回修改的內(nèi)容,無法驗證是否更改
//????????$res?=?User::update([
//????????????'id'?=>?1,
//????????????'username'?=>?'wenco_update'
//????????]);
//????????dump($res->toArray());

????????#當(dāng)數(shù)據(jù)中不存在自增id時如下,第二個參數(shù)即為where,可以為閉包函數(shù),返回修改內(nèi)容
//????????????????$res?=?User::update([
//????????????'username'?=>?'wenco_update'
//??當(dāng)?shù)诙€參數(shù)為數(shù)組??????],['id'?=>?'2']);
//當(dāng)?shù)诙€參數(shù)為閉包函數(shù)????????],function?($query){
//????????????????????$query->where('id','>=',?15);
//????????????????});

????????#where?返回被影響行數(shù),推薦使用!
//????????$res?=?User::where('id','between',[1,5])->update([
//????????????'username'?=>?'wenco_old'
//????????]);
//????????dump($res);

????????#User::get,返回被影響行數(shù)
//????????$userModel?=?User::get(1);
//????????$userModel->username?=?'123';
//????????$userModel->email?=?'123@qq.com';
//????????$res?=?$userModel->save();
//????????dump($res);

????????#new?User,返回影響行數(shù),第二個參數(shù)可以為閉包函數(shù)
//????????$userModel?=?new?User;
//????????$res?=?$userModel->save([
//????????????'username'?=>?555
//????????],['id'?=>??18]);
//????????dump($res);

????????#saveAll,返回結(jié)果集,不推薦使用
????????$userModel?=?new?User;
????????$res?=?$userModel->saveAll([
????????????['id'?=>?1,'username'?=>?'wencocococ'?],
????????????['id'?=>?2,'username'?=>?'wencocococ'?]
????????]);
????????dump($res);
????}
}


2
采集 2

向數(shù)據(jù)庫中添加數(shù)據(jù):

<?php
namespace?app\index\controller;
use?think\controller;
use?app\index\model\User;

class?Index
{
????public?function?index()
????{
????????#user::create(),當(dāng)?shù)诙€參數(shù)不為數(shù)組而是true時會自動過濾掉表中沒有的字段的數(shù)據(jù),第二個參數(shù)為字段數(shù)組,表示只允許在這兩個字段內(nèi)添加數(shù)據(jù)
//????????User::create([
//????????????'username'??=>??'wenco_1',
//????????????'password'??=>??md5('wenco_1'),
//????????????'email'?????=>??'wenco_1@qq.com',
//????????????'num'???????=>??100,
//????????????'demo'??????=>??123
//????????],['username','email']);

//????????$userModel?=?new?User;
//????????$userModel->username?=?'wenco_2';
//????????$userModel->password?=?md5('wenco_2');
//????????$userModel->save();
//????????dump($userModel->id);

????????#直接使用save(),返回被影響行數(shù),allowField()與上面create的第二個參數(shù)功能及用法一樣
//????????$userModel?=?new?User;
//????????$res?=?$userModel->allowField(true)->save([
//????????????'username'????=>??"wenco_5",
//????????????'password'???=>??md5('wenco_5'),
//????????????'email'??????=>???'wenco_5@qq.com'
//????????]);

????????#使用saveAll同時傳遞多條數(shù)據(jù)
????????$userModel?=?new?User;
????????$res?=?$userModel->allowField(true)->saveAll([
????????????['email'?=>?'wenco_8@qq.com'],
????????????['email'?=>?'wenco_9@qq.com'],
????????????['email'?=>?'wenco_10@qq.com']
????????]);
????????foreach?($res?as?$val){
????????????dump($val->?toArray());
????????}
????????}
}


0
采集 1

模型獲取數(shù)據(jù)庫數(shù)據(jù):

<?php
namespace?app\index\controller;
use?think\controller;
use?app\index\model\User;

class?Index
{
????public?function?index(){
????????#通過get方式獲取數(shù)據(jù),括號內(nèi)可以為主鍵,也可以為閉包函數(shù)
//????????$res?=?User::get(function($query){
//????????????$query->where("username","eq","wenco_3")
//????????????->field("email");
//????????});

//????????$res?=?User::where("id","<=",15)
//????????????->field("id,username")
//????????????->limit(19)
//????????????->order("id"??,"DESC")
//????????????->select();

????????#user::all()
//????????$res?=?User::all(function($query){
//????????????$query->where("id","<","5")
//????????????????->field("id","username");
//????????});
????????#column()第二個參數(shù)為新生成的數(shù)組的下標(biāo)
????????$res?=?User::column("email","username");

//????????foreach($res?as?$val){
//????????????dump($val->toArray());
//????????}

????????//$res?=?$res->toArray();//將結(jié)果轉(zhuǎn)換成數(shù)組
????????dump($res);
????}
}


0
采集 0

3-1 什么是模型

2018-05-07

首先創(chuàng)建一個Use模型,要繼承think下的Model:

<?php
namespace?app\index\model;
use?think\Model;
class?User?extends?Model{
}

然后在控制器中引用:

<?php
namespace?app\index\controller;
use?think\controller;
use?app\index\model\User;

class?Index
{
????public?function?index(){
????????#new對象
//????????$user?=?new?User;
//????????$res?=?$user::get(5);

????????#$user?=?loader::model():需要use?think下的Loader?//use?think\Loader;
//????????$res?=?Loader::model("User");
//????????$res?=?User::get(2);

????????#助手函數(shù)model()
//????????$user?=?model("User");
//????????$res?=?$user::get(4);

????????$res?=?$res->toArray();//將結(jié)果轉(zhuǎn)換成數(shù)組
????????dump($res);
????}
}


1
采集 3
首頁上一頁12下一頁尾頁

快速入門ThinkPHP 5.0 --模型篇

  • 難度中級
  • 時長 3小時 0分
  • 人數(shù)1812
  • 評分9.7

一個標(biāo)準(zhǔn)的網(wǎng)站一定離不開數(shù)據(jù)庫的操作,在本套課程中我和你一起來揭開ThinkPHP5 數(shù)據(jù)操作的神秘面紗,和你一起愉快的使用 ThinkPHP5 操作數(shù)據(jù)庫,讓數(shù)據(jù)庫操作變的更愉悅。

烏云龍 PHP開發(fā)工程師

愛拼 專注 才會贏

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號