1、with的使用
Thinkphp可以進行關聯操作,數據庫中需要用到join連接查詢時候,用thinkPHP框架的關聯查詢可以有效的提高查詢效率,下面是常用的關聯:
(1)hasOne:有一個,A 有一個 B(一對一關聯)
(2)hasMany:有很多,A 有很多 B(一對多關聯)
(3)belongsTo: 多個(或一個)A 屬於 B(屬於,相當與多對一)
(4)belongsToMany:多對多關聯
這里有兩個表:comment(評論表)、article(文章表)
#文章 create table article ( id int primary key AUTO_INCREMENT comment "ID", title varchar(255) comment '標題', content text comment '內容', ) #評論 create table comment ( id int primary key AUTO_INCREMENT comment "ID", msg varchar(255) comment '評論內容', article_id int comment '文章ID',
art_sum int )
關聯查詢
comment的模型使用belongsTo()方法關聯article表:
//評論表 class Comment extends Model { public function comm() { // Article::class關聯的表,article_title自定義字段,title顯示的字段 return $this->belongsTo(Article::class)->bind([ "article_title"=>"title" ]); //不設置bind的全部顯示 // return $this->belongsTo(Article::class); } }
控制層使用with:
public function demo2(){ $items = Comment::with(["comm"])->select()->toArray(); echo "<pre>"; print_r($items); }
關聯刪除
方法一:belongsTo,多對一
comment的模型關聯article:
//評論表 class Comment extends Model { public function comm() { return $this->belongsTo(Article::class); } }
together關聯刪除:
public function demo1(){ //Comment數據:ID=1,article_id=2,即刪除Comment.ID等於1的數據,同時刪除article.ID等於2的數據 $items = Comment::with(["comm"])->find(1); $items->together(["comm"])->delete();//刪除 }
從上面可以看到,關聯刪除根據comment.article_id,如果沒有comment里article_id字段,article的數據也不會刪除。
comment是多個對應一個article,根據comment關聯刪除就沒有對應的article,我們想要的效果是根據article對應的comment,一對多的刪除。
方法二:hasMany,一對多(推薦)
article模型關聯comment:
class Article extends Model { //belongsTo是多對一,不適合使用 // public function comment() { // return $this->belongsTo(Comment::class); // } public function comment() { return $this->hasMany(Comment::class,"article_id"); } }
together關聯刪除:
//關聯刪除 public function demo3(){ $list = Article::with('comment')->find(1); $list->together(["comment"])->delete(); }
除了關聯刪除和關聯查詢,還有withCount(關聯數量統計的個數):
//關聯查詢的數量 public function demo2(){ // self::withCount('關聯方法名')->select(); // self::withCount(['關聯方法名' => '自定義屬性名'])->select(); $list = Article::withCount('comment')->select(); foreach($list as $user){ // 獲取文章關聯的評論關聯個數 echo $user->comment_count; echo "<br>"; } }
withSum(關聯數量的相加的結果):
//關聯查詢的統計 public function demo4(){ //comment的art_sum指定關聯統計的字段 $list = Article::withSum('comment',"art_sum")->select(); foreach($list as $user){ // 獲取文章關聯的評論的art_sum相加的結果 echo $user->comment_sum; echo "<br>"; } }
注:1、withCount的輸出采用“關聯方法名_count”,另外withMax()、withMin()、withSum()、withAvg()均可支持這個方法。
2、除了withCount不需要指定字段,其他都要指定統計字段
2、insertAll 批量增加
添加多條數據直接向 Db 類的 insertAll 方法傳入需要添加的數據即可
$data = [ ['foo' => 'bar', 'bar' => 'foo'], ['foo' => 'bar1', 'bar' => 'foo1'], ['foo' => 'bar2', 'bar' => 'foo2'] ]; Db::name('user')->insertAll($data);
確保要批量添加的數據字段是一致,順序一致(如上,foo和bar順序一致),格式一致(格式如上面)
3、hasWhere關聯條件查詢
comment的模型關聯article:
//評論表 class Comment extends Model { public function article() { //不設置bind的全部顯示 return $this->belongsTo(Article::class)->bind([ "article_title"=>"title" ]); } }
控制器:
//使用hasWhere根據article的條件查詢(注:comment與article有關聯),同時使用with把article查詢出來: public function demo5(){ //方法一 $list = Comment::hasWhere('article',[["title","like","%美國%"]])->with("article")->select()->toArray(); //方法二 $list = Comment::with(['article'=>function($query){ $query->where("title","like","%量子%"); }])->select(); halt($list); }
注:haswhere的第1個參數模型關聯方法名,模型關聯方法名和模型名稱一樣,否則報錯
