ThinkPHP資料庫增刪改查
本文例項講述了thinkPHP資料庫增刪改查操作方法。分享給大家供大家參考,具體如下:
thinkphp對資料庫增刪改查進行了封裝操作,使得使用更加方便,但是不一定靈活。
可以用封裝的用,需要寫sql,可以執行sql。
1.原始的 $Model = new Model(); // 例項化一個model物件 沒有對應任何資料表 $insert_sql = "INSERT INTO sh_wxuser_collection (user_id,store_id,good_id,addtime) VALUES('".$user_id."','".$store_id."','".$good_id."','".$addtime."');"; $Model - >query($insert_sql);
2.針對表例項化的,這裡的表原名是sh_wxuser_collection。sh是字首。 $model = M('wxuser_collection'); //自動省去sh $insert_sql = "INSERT INTO __TABLE__ (user_id,store_id,good_id,addtime) VALUES('".$user_id."','".$store_id."','".$good_id."','".$addtime."');"; $model - >query($insert_sql);
另一種寫法,_可以寫成大寫,它會自動轉化成_ $model = M('WxuserCollection'); //自動省去sh $insert_sql = "INSERT INTO __TABLE__ (user_id,store_id,good_id,addtime) VALUES('".$user_id."','".$store_id."','".$good_id."','".$addtime."');"; $model - >query($insert_sql);
3. 封裝的add語句 $model = M('WxuserCollection'); $data = array('user_id' = >$user_id, 'store_id' = >$store_id, 'good_id' = >$good_id, 'addtime' = >$addtime); $model - >data($data) - >add();
4.封裝的修改edit語句 $model = M('WxuserCollection'); $data = array('user_id' = >$user_id, 'store_id' = >$store_id, 'good_id' = >$good_id, 'addtime' = >$addtime); $model - >data($data) - >where('id=3') - >save();
確實挺方便的,但是方便之餘,別忘了原始的sql,原汁原味的sql,才最有意思。
5.find() $model = M('WxuserCollection'); $res1 = $model - >find(1); $res2 = $model - >find(2); $res3 = $model - >where('good_id=1105 AND store_id = 1 AND user_id = 20') - >find();
find獲取一條資料,find(1)獲取id為1的資料,find(2)獲取id為2的資料。最後一個是獲取條件為where的中的第一條資料。
5.select() $model = M('WxuserCollection'); $res = $model - >where('good_id=1105 AND store_id = 1 AND user_id = 20') - >field('id,good_id as good') - >select();
獲取所有資料。這裡的好處就是,不用考慮sql語句的順序了,隨心所欲呼叫函式就可以了。
6.delete() $model = M('WxuserCollection'); $res = $model - >where('id=1') - >delete(); // 成功返回1 失敗返回0
根據條件進行刪除操作
7.field() $model = M('WxuserCollection'); $res = $model - >field('id,good_id as good') - >select(); $res = $model - >field(array('id', 'good_id' = >'good')) - >select(); $res = $model - >field('id', true) - >select();
字串,陣列兩種方式,第三個是表示獲取處理id之外的所有欄位。
8.order() $model = M('WxuserCollection'); $res = $model - >order('id desc') - >select(); $res = $model - >order('id asc') - >select(); $res = $model - >order(array('id' = >'desc')) - >select(); $res = $model - >order(array('id')) - >select();
字串,陣列兩種方式,預設asc。
9.join() $Model->join(' work ON artist.id = work.artist_id')->join('card ON artist.card_id = card.id')->select(); $Model->join('RIGHT JOIN work ON artist.id = work.artist_id')->select(); $Model->join(array(' work ON artist.id = work.artist_id','card ON artist.card_id = card.id'))->select();
預設採用LEFT JOIN 方式,如果需要用其他的JOIN方式,可以改成第二種,
如果join方法的引數用陣列的話,只能使用一次join方法,並且不能和字串方式混合使用。
10.setInc() $User = M("User"); // 例項化User物件 $User->where('id=5')->setInc('score',3); // 使用者的積分加3 $User->where('id=5')->setInc('score'); // 使用者的積分加1 $User->where('id=5')->setDec('score',5); // 使用者的積分減5 $User->where('id=5')->setDec('score'); // 使用者的積分減1
11.getField()
獲取某個欄位值 $User = M("User"); // 例項化User物件 // 獲取ID為3的使用者的暱稱 $nickname = $User->where('id=3')->getField('nickname');
返回的nickname是一個字串結果。也就是說,即使有滿足條件的多個欄位,也只會返回一個結果。
獲取某個欄位列
如果希望返回符合要求的欄位列(多個結果),可以使用: $User = M("User"); // 例項化User物件 // 獲取status為1的使用者的暱稱列表 $nickname = $User->where('status=1')->getField('nickname',true);
第二個引數傳入了true,返回的nickname則是一個數組,包含了所有滿足條件的暱稱列表。
如果需要限制返回結果數量,可以使用: $nickname = $User->where('status=1')->getField('nickname',8);
獲取2個欄位列表 $User = M("User"); // 例項化User物件 // 獲取status為1的使用者的暱稱列表 $nickname = $User->where('status=1')->getField('id,nickname');
如果getField方法傳入多個欄位名稱的話,預設返回一個關聯陣列,以第一個欄位的值為索引(所以第一個欄位要儘量選擇不會重複的)。
獲取多個欄位列表 $result = $User->where('status=1')->getField('id,account,nickname');
如果傳入了2個以上的欄位名,則返回一個二維陣列(類似select方法的返回值,區別在於索引是二維陣列的鍵名是第一個欄位的值)
綜合使用案例 $where = array('a.store_id' => $this->store_id, 'a.user_id' => $this->user_id); $collects = $this->collectModel->table("sh_wxuser_collection a")->field(array('b.name','b.price','b.oprice','b.logoimg','a.goods_id'))->limit($start, $offset)->order('a.addtime DESC')->where($where)->join(' sh_goods b ON a.goods_id = b.id')->select();// 獲取當前頁的記錄 echo M()->getLastSql(); // 除錯sql語句用 $count = $this->collectModel->table("sh_wxuser_collection a")->where($where)->count(); // 獲取總的記錄數
這裡由於結合了兩張表,所以用到了table方法,重新定義表名,相應的條件和引數都要加上字首。a. 或者b.
其中field欄位要麼是一個字串,要麼是陣列。 field('b.name', 'b.price', 'b.oprice', 'b.logoimg', 'a.goods_id') // 錯誤
我之前就這麼寫,問題大大的。
使用框架,就不能靈活的寫sql了。不過對sql有一個深刻的認識,也有利於靈活的使用好框架。
用於除錯sql語句的方法。 echo M()->getLastSql();
很方便。