thinkphp database additions and deletions to the operation Method Example detailed

Source: Internet
Author: User
This paper describes the operation method of adding and deleting thinkphp database. Share to everyone for your reference, as follows:

thinkphp the database additions and deletions to the package operation, making the use more convenient, but not necessarily flexible.

Can be used for encapsulation, you need to write SQL, you can execute SQL.

1. The original

$Model = new Model (); Instantiating a model object does not correspond to any data table $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. For the table instantiation, the table here is formerly known as Sh_wxuser_collection. SH is the prefix.

$model = M (' wxuser_collection '); Automatically omit 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);


Another way of writing, _ can be written in uppercase, it will automatically translate into _

$model = M (' wxusercollection '); Automatically omit 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. Encapsulated Add statement

$model = M (' wxusercollection '); $data = Array (' user_id ' = > $user _id, ' store_id ' = > $store _id, ' good_id ' = > $good _i D, ' addtime ' = > $addtime); $model->data ($data)->add ();


4. The modified edit statement for the package

$model = M (' wxusercollection '); $data = Array (' user_id ' = > $user _id, ' store_id ' = > $store _id, ' good_id ' = > $good _i D, ' addtime ' = > $addtime); $model->data ($data)->where (' id=3 ')->save ();


is very convenient, but convenient, do not forget the original SQL, authentic SQL, the most interesting.

5.find ()

$model = M (' wxusercollection '); $res 1 = $model->find (1); $res 2 = $model->find (2); $res 3 = $model->where (' Good_ id=1105 and store_id = 1 and user_id = ")->find ();


Find gets a piece of data, find (1) Gets the data with ID 1, and find (2) Gets the data with ID 2. The last one is to get the first piece of data in the Where condition.

5.select ()

$model = M (' wxusercollection '); $res = $model->where (' good_id=1105 and store_id = 1 and user_id = + ')->field (' ID , good_id as good ')->select ();


Get all the data. The advantage here is that you don't have to consider the order of the SQL statements, you can call the function as you like.

6.delete ()

$model = M (' wxusercollection '); $res = $model->where (' id=1 ')->delete (); Successful return 1 failed return 0


Delete operations based on conditions


7.field ()

$model = M (' wxusercollection '), $res = $model->field (' id,good_id as Good ')->select (); $res = $model->field (AR Ray (' id ', ' good_id ' = > ' good '))->select (); $res = $model->field (' id ', true)->select ();


A string, an array of two ways, and a third is any field that represents the Get processing ID.

8.order ()

$model = M (' wxusercollection '); $res = $model->order (' id desc ')->select (); $res = $model->order (' ID ASC ')-& Gt;select (); $res = $model->order (Array (' id ' = > ' desc '))->select (); $res = $model->order (Array (' ID '))-&G T;select ();


String, array of two ways, default ASC.

9.join ()

$Model->join (' work in 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 (' work on artist.id = work.artist_id ', ' card on artist.card_id = Card.id '))->select ();


By default, the left join method, if you need to use a different join method, you can change to the second type,

The Join method can only be used once if the parameters of the join method are in an array, and cannot be mixed with the string method.

10.SETINC ()

$User = M ("User"); Instantiate the User object $user->where (' id=5 ')->setinc (' Score ', 3); The user's points plus 3$user->where (' id=5 ')->setinc (' score '); User's points plus 1$user->where (' id=5 ')->setdec (' Score ', 5); User's points minus 5$user->where (' id=5 ')->setdec (' score '); User's points minus 1


11.getField ()

Get a field value

$User = M ("User"); Instantiate the User object//Get the nickname for the username ID 3 $nickname = $User->where (' id=3 ')->getfield (' nickname ');


The returned nickname is a string result. That is, even if there are multiple fields that satisfy the condition, only one result is returned.

Get a field column

If you want to return columns (multiple results) that meet the requirements, you can use:

$User = M ("User"); Instantiate the User object//Get the nickname list of the users of status 1 $nickname = $User->where (' Status=1 ')->getfield (' nickname ', true);


The second argument passes true, and the returned nickname is an array that contains all the list of nicknames that meet the criteria.

If you need to limit the number of returned results, you can use:

$nickname = $User->where (' Status=1 ')->getfield (' nickname ', 8);


Get a list of 2 fields

$User = M ("User"); Instantiate the User object//Get the nickname list of the users of status 1 $nickname = $User->where (' Status=1 ')->getfield (' Id,nickname ');


If the GetField method passes in more than one field name, it returns an associative array by default, with the value of the first field indexed (so the first field should be selected as not to be duplicated).

Get multiple field lists

$result = $User->where (' Status=1 ')->getfield (' Id,account,nickname ');


If more than 2 field names are passed in, a two-dimensional array is returned (similar to the return value of the Select method, except that the index is the value of the first field for the key name of the two-dimensional array)

Comprehensive Use cases

$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 ();//Gets the record of the current page echo M ()->getlastsql (); Debug SQL statements with $count = $this->collectmodel->table ("Sh_wxuser_collection a")->where ($where)->count (); Get the total number of records


Because of the combination of the two tables, the table method is used to redefine the name, and the corresponding conditions and parameters are prefixed. A. or b.

Where field fields are either a string or an array.

Field (' B.name ', ' b.price ', ' b.oprice ', ' b.logoimg ', ' a.goods_id ')//Error

I've been writing this before, and it's a big problem.

With the framework, you can't write SQL flexibly. However, there is a deep understanding of SQL, but also conducive to the flexible use of good framework.

The method used to debug the SQL statement.

Echo M ()->getlastsql ();

It is hoped that this article is helpful to the PHP program design based on thinkphp framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.