溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Thinkphp框架對數(shù)據(jù)庫的操作有哪些

發(fā)布時間:2021-01-16 10:21:40 來源:億速云 閱讀:231 作者:小新 欄目:編程語言

這篇文章主要介紹了Thinkphp框架對數(shù)據(jù)庫的操作有哪些,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

1.添加數(shù)據(jù)

1.1添加一條數(shù)據(jù)

$user           = new User;
$user->name     = 'thinkphp';
$user->email    = 'thinkphp@qq.com';
$user->save();
$user = new User;
$user->save([
    'name'  =>  'thinkphp',
    'email' =>  'thinkphp@qq.com'
]);

1.2過濾非數(shù)據(jù)表字段的數(shù)據(jù)

$user = new User;
// 過濾post數(shù)組中的非數(shù)據(jù)表字段數(shù)據(jù)
$user->allowField(true)->save($_POST);

1.3指定某些字段數(shù)據(jù)

$user = new User;
// post數(shù)組中只有name和email字段會寫入
$user->allowField(['name','email'])->save($_POST);

1.4添加多條數(shù)據(jù)

$user = new User;
$list = [
    ['name'=>'thinkphp','email'=>'thinkphp@qq.com'],
    ['name'=>'onethink','email'=>'onethink@qq.com']
];
$user->saveAll($list);

1.5靜態(tài)方法

$user = User::create([
    'name'  =>  'thinkphp',
    'email' =>  'thinkphp@qq.com'
]);
echo $user->name;
echo $user->email;
echo $user->id; // 獲取自增ID

2.更新數(shù)據(jù)

2.1查找并更新

$user = User::get(1);
$user->name     = 'thinkphp';
$user->email    = 'thinkphp@qq.com';
$user->save();

2.2直接更新數(shù)據(jù)

$user = new User;
// save方法第二個參數(shù)為更新條件
$user->save([
    'name'  => 'thinkphp',
    'email' => 'thinkphp@qq.com'
],['id' => 1]);

2.3 過濾非數(shù)據(jù)表字段

$user = new User;
// 過濾post數(shù)組中的非數(shù)據(jù)表字段數(shù)據(jù)
$user->allowField(true)->save($_POST,['id' => 1]);

2.4指定某些字段

$user = new User();
// post數(shù)組中只有name和email字段會寫入
$user->allowField(['name','email'])->save($_POST, ['id' => 1]);

2.5批量更新數(shù)據(jù)

$user = new User;
$list = [
    ['id'=>1, 'name'=>'thinkphp', 'email'=>'thinkphp@qq.com'],
    ['id'=>2, 'name'=>'onethink', 'email'=>'onethink@qq.com']
];
$user->saveAll($list);

2.6靜態(tài)方法

User::where('id', 1)
    ->update(['name' => 'thinkphp']);

2.7自動識別

2.7.1顯示更新數(shù)據(jù)

// 實例化模型
$user = new User;
// 顯式指定更新數(shù)據(jù)操作
$user->isUpdate(true)
    ->save(['id' => 1, 'name' => 'thinkphp']);

2.7.2顯示新增數(shù)據(jù)

$user = User::get(1);
$user->name = 'thinkphp';
// 顯式指定當前操作為新增操作
$user->isUpdate(false)->save();

3.刪除數(shù)據(jù)

3.1刪除當前模型

$user = User::get(1);
$user->delete();

3.2根據(jù)主鍵刪除

User::destroy(1);
// 支持批量刪除多個數(shù)據(jù)
User::destroy('1,2,3');
// 或者
User::destroy([1,2,3]);

3.3條件刪除

User::destroy(function($query){
    $query->where('id','>',10);
});

4.查詢數(shù)據(jù)

4.1獲取單個數(shù)據(jù)

// 取出主鍵為1的數(shù)據(jù)
$user = User::get(1);
echo $user->name;
// 使用查詢構(gòu)造器查詢滿足條件的數(shù)據(jù)
$user = User::where('name', 'thinkphp')->find();
echo $user->name;

4.2獲取多個數(shù)據(jù)

// 根據(jù)主鍵獲取多個數(shù)據(jù)
$list = User::all('1,2,3');
// 或者使用數(shù)組
$list = User::all([1,2,3]);
// 對數(shù)據(jù)集進行遍歷操作
foreach($list as $key=>$user){
    echo $user->name;
}
// 使用查詢構(gòu)造器查詢
$list = User::where('status', 1)->limit(3)->order('id', 'asc')->select();
foreach($list as $key=>$user){
    echo $user->name;
}

4.3獲取某個字段或者某個列的值

// 獲取某個用戶的積分
User::where('id',10)->value('score');
// 獲取某個列的所有值
User::where('status',1)->column('name');
// 以id為索引
User::where('status',1)->column('name','id');

感謝你能夠認真閱讀完這篇文章,希望小編分享的“Thinkphp框架對數(shù)據(jù)庫的操作有哪些”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,更多相關知識等著你來學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI