-
tp里面更新數(shù)據(jù)的兩種方式
1.
$model::where(array('字段'=>'值'))->update(array($change));?
或者如圖所示;
查看全部 -
use think\Db;
$db = Db::name('table');
$db->insert($data); //返回值是影響記錄的行數(shù) 插入數(shù)
$db->insertGetId($data); //返回插入數(shù)的自增id
$db->insertAll($data); //返回插入的成功行數(shù)
查看全部 -
軟刪除:
首先在模型里面引用SoftDelete
<?php
namespace app\index\model;
use think\Model;
use traits\model\SoftDelete;
class User extends Model{
? ? use SoftDelete;
#刪除數(shù)據(jù)的時(shí)候刪除時(shí)間戳默認(rèn)寫入字段delete_time中,當(dāng)要自定義時(shí):
// ? ?protected $deleteTime = '自定義刪除時(shí)間字段名';
}
然后在控制器里面執(zhí)行操作:
?public function index(){
// ? ? ? ?$res = User::destroy(4);//被軟刪除
// ? ? ? ?$res = User::get(4);//返回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的第二個(gè)參數(shù)后面?zhèn)魅胍粋€(gè)true值
? ? ? ? $res = User::destroy(1,true);
?
? ? ? ? #通過get方式進(jìn)行軟刪除/刪除
? ? ? ? $res = User::get(3);//如果此處數(shù)據(jù)已經(jīng)被軟刪除則獲取到的為NULL,后面的操作無效
? ? ? ? $user->delete();//軟刪除
? ? ? ? $res = $user->delete(true);//刪除
? ? }
查看全部 -
全局添加更新時(shí)間需要在表中新建create_time和update_time字段,配置文件中設(shè)置“auto_timestamp"=>true,一般不開啟這個(gè)功能,否則表中不存在該字段時(shí)會(huì)報(bào)錯(cuò),真正刪除需要把destroy方法或delete方法的第二個(gè)參數(shù)設(shè)置為true
123456789 use
?think\Model;
use
?traits\model\SoftDelete;
class
?User?
extends
?Model{
????
use
?SoftDelete;使用軟刪除
????
protected
?$autoWriteTimestamp
?=?true;
//開啟自動(dòng)更新時(shí)間
????
protected
?$createTime
?=?false;
//設(shè)置為false代表不使用創(chuàng)建時(shí)間
????
protected
?$updateTime
?=?
"update_at"
;
//設(shè)置自定義更新時(shí)間的字段
????
protected
?$deleteTime
?=?
"delete_at"
;
//設(shè)置自定義的軟刪除字段
}
查看全部 -
auto (新增及更新的時(shí)候自動(dòng)完成的屬性數(shù)組)
insert(僅新增的時(shí)候自動(dòng)完成的屬性數(shù)組)
update(僅更新的時(shí)候自動(dòng)完成的屬性數(shù)組)
查看全部 -
<?php
????
namespace
?app\index\model;
????
use
?think\Model;
????
class
?User?
extends
?Model{
????????
protected
?$auto
?=?[
????????????
'time'
????????
];
????????
protected
?$insert
?=?[
????????????
'time_insert'
????????
];
????????
protected
?$update
?=?[
????????????
'time_update'
????????
];
????????
public
?function
?setPasswordAttr(
$val
,
$data
){
????????????
//第二個(gè)參數(shù)是數(shù)組數(shù)據(jù)
????????????
return
?$val
.
$data
[
'email'
];
????????????
return
?md5(
$val
);
????????
}
????????
public
?function
?setTimeAttr(){
????????????
return
?time();
????????
}
????????
public
?function
?setTimeInsertAttr(){
????????????
return
?time();
????????
}
????????
public
?function
?setTimeUpdateAttr(){
????????????
return
?time();
????????
}
????
}
查看全部 -
<?php
????
namespace
?app\index\model;
????
use
?think\Model;
????
class
?User?
extends
?Model{
????????
public
?function
?getSexAttr(
$val
){
????????????
switch
(
$val
){
????????????????
case
?'1'
:
????????????????????
return
?"男"
;
????????????????????
break
;
????????????????
case
?'2'
:
????????????????????
return
?"女"
;
????????????????????
break
;
????????????????
default
:
????????????????????
return
?"未知"
;
????????????????????
break
;
????????????
}
????????
}
????
}
查看全部 -
模型獲取器 成員方法(get字段名Attr)[字段名首字母大寫] 在model中使用 在controller中use下model中的類(該類需繼承model類),利用 ?類名::get() ?進(jìn)行獲取數(shù)據(jù)值 可利用toArray獲取改后數(shù)據(jù) 可利用getData獲取改前數(shù)據(jù)
查看全部 -
count ? ? User::count(); ? ? ? ? max ? ? ? User::max('字段名') ? sum ? ? ? User::sum('字段名') ? avg ? ? ? User::avg('字段名') ? min ? ? ? User::min('字段名') 都可以和where條件合作
查看全部 -
destroy對(duì)數(shù)據(jù)庫刪除的操作 $userModel = User::get(7); ?$res = $userModel->delete();//對(duì)數(shù)據(jù)庫刪除的操作 $res = User::where("1=1") -> delete();//1=1 清空數(shù)據(jù)庫
查看全部 -
<?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
);
????
}
}
查看全部 -
<?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í),數(shù)據(jù)更新方式,返回修改的內(nèi)容,無法驗(yàn)證是否更改
//????????$res?=?User::update([
//????????????'id'?=>?1,
//????????????'username'?=>?'wenco_update'
//????????]);
//????????dump($res->toArray());
?????????
#當(dāng)數(shù)據(jù)中不存在自增id時(shí)如下,第二個(gè)參數(shù)即為where,可以為閉包函數(shù),返回修改內(nèi)容
//????????????????$res?=?User::update([
//????????????'username'?=>?'wenco_update'
//??當(dāng)?shù)诙€(gè)參數(shù)為數(shù)組??????],['id'?=>?'2']);
//當(dāng)?shù)诙€(gè)參數(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ù),第二個(gè)參數(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
);
????
}
}
查看全部 -
// $res = User::create([ // 'username' => 'imooc', // 'password' => md5('imooc'), // 'email' ? ?=> 'imooc@qq.com', // 'num' ? ? ?=> 100, // 'demo' ? ? => 123 // ], ['username', 'email']); // $userModel = new User; // $userModel->username = '17771258'; // $userModel->email = '17771258@qq.com'; // $userModel->password = md5('17771258'); // $userModel->save(); // $userModel = new User; // $res = $userModel // ->allowField('username') // ->save([ // 'username' ?=> 'imooc1', // 'password' ?=> md5('imooc1'), // 'demo' ? ? ?=> 123 // ]); $userModel = new User; $res = $userModel->saveAll([ ['email'=> '17771258@qq.com'], ['email'=> '17771259@qq.com'] ]); foreach($res as $val) { dump($val->toArray()); } //dump($res); //dump($res);
查看全部 -
Model->get 返回對(duì)象 用toarray 獲取信息
查看全部 -
使用模型查詢數(shù)據(jù);#通過get方式獲取數(shù)據(jù),括號(hào)內(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();
查看全部
舉報(bào)