Examples of thinkphp database additions and deletions and checking operation methods _php

Source: Internet
Author: User
Tags php template codeigniter zend framework

This paper gives an example of the thinkphp database and the operation method of checking and deleting. Share to everyone for your reference, specific as follows:

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

You can use encapsulation, you need to write SQL, you can execute SQL.

1. The original

$Model = new Model (); Instantiation of 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 table instantiation , the table here is originally called 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 convert to _

$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. The encapsulated Add statement

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

4. Encapsulated modify Edit statement

$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 ();

Really convenient, but convenient, don't 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 data in the where the condition is.

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 ();

Gets all the data. The advantage here is that you do not have to consider the order of the SQL statements, you can call the function at will.

6.delete ()

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

Delete operations based on criteria

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 ();

String, array two ways, and the third is to get all the fields except the processing 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 ();

String, array two ways, default 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 (' 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 another join method, can be changed to the second type,

If the parameters of the join method are in an array, only one join method can be used and cannot be mixed with the string.

10.SETINC ()

$User = M ("User"); Instantiate the User Object
$User->where (' id=5 ')->setinc (' Score ', 3);//user's integral 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 of a 3-id
$nickname = $User->where (' id=3 ')->getfield (' nickname ');

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

Get a field column

If you want to return a character Chelle (multiple results) that match your requirements, you can use:

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

The second argument passes true, and the returned nickname is an array containing all the nickname lists 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 with status 1
= $User->where (' Status=1 ')->getfield (' Id,nickname ');

If the GetField method passes in more than one field name, the default returns an associative array, indexed by the value of the first field (so the first field should try to choose not to repeat).

Get more than one field list

$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 case

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

Because we combine two tables, we use the table method, redefine the table name, and prefix the corresponding conditions and parameters. A. or b.

The field field is either a string or an array.

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

That's what I wrote before, 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 flexible use of a good framework.

The method used to debug the SQL statement.

Echo M ()->getlastsql ();

Very convenient.

More interested readers of thinkphp related content can view the website topic: "thinkphp Introductory Course", "thinkphp template Operation Skill Summary", "thinkphp Common Method Summary", "The CodeIgniter Introductory Course", "CI ( CodeIgniter) Framework Advanced tutorials, introductory tutorials for the Zend framework Framework, Smarty Templates Primer tutorial, and PHP template technology summary.

I hope this article will help you with 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.