thinkphp database operations, inserts, updates, deletes, queries

Source: Internet
Author: User

Thinkphp provides flexible and convenient data manipulation methods, not only realize the four basic operations of database operations (CURD): Create, read, update and delete the implementation, but also built-in a lot of practical data manipulation methods, providing the best experience of activerecords mode.

New record

PHP Code

1. $User->find (2);

2. $User->delete (); To delete a found record

3. $User->delete (' 5,6 '); Delete a primary key of 5, 6 data

4. $User->deleteall (); Delete all data from a query

PHP Code

1.//Instantiate a user model object

2.

3. $User = new Usermodel ();

4.//Then assign a value to the data object

5. $User->name = ' thinkphp ';

6. $User->email = ' ThinkPHP@gmail.com ';

7.//Then you can save the new user object.

8. $User->add ();

9.//If you need to lock the instantiation model object to pass in the data, you can use the

$data [' name '] = ' thinkphp ';

$data [' email '] = ' ThinkPHP@gmail.com ';

$User = new Usermodel ($data);

$User->add ();

14.//or pass directly to the new data in the Add method

$data [' name '] = ' thinkphp ';

$data [' email '] = ' ThinkPHP@gmail.com ';

$User = new Usermodel ();

$User->add ($data);

19.

20.

In general, the data objects in the application are not likely to be written by hand, but rather have the process of creating a data object. Thinkphp provides a create method that creates a data object and then makes other additions or edits.

PHP Code

1. $User = D ("User");

2. $User->create (); Create a user data object that is created by default with data submitted by the form

3. $User->add (); New Form submission Data

The Create method supports creating data objects from other ways, for example, from other data objects, or arrays, etc.

PHP Code

1. $data [' name '] = ' thinkphp ';

2. $data [' email '] = ' ThinkPHP@gmail.com ';

3. $User->create ($data);

4.//Create a new member data object from the user data object

5. $Member = D ("member");

6. $Member->create ($User);

Support for more than one new record

PHP Code

1. $User = new Usermodel ();

2. $data [0][' name '] = ' thinkphp ';

3. $data [0][' email '] = ' ThinkPHP@gmail.com ';

4. $data [1][' name '] = ' fleeting ';

5. $data [1][' email '] = ' liu21st@gmail.com ';

6. $User->addall ($data);

Under the MySQL database, an SQL statement is automatically used to implement multiple data insertions.

Query records

Reading the records of a database I think it's the most interesting thing in database operations, and everyone who has written a text database knows that saving and deleting data is easy (it's just a matter of specification and efficiency) and it's hard to find the data you need in a variety of ways. Thinkphp through a variety of efforts to make the database query operation becomes easy, also let thinkphp become rich content.

Thinkphp has a very clear agreement, is that a single data query and multiple data query methods are separate, or you may think that sometimes you do not know whether to query the data is a single or multiple, but one thing is clear, you need to return a data or want to return a dataset. Because the two types of return data are operating in a very different way, no matter how the return, we can directly in the model object operation, of course, as well as data can be passed to the variables you need.

First, for the simplest example, if we were to query a user record with a primary key of 8, we could use some of the following methods:

PHP Code

1. $User->find (8);

This is the most intuitive query language, if the query is successful, the query results are stored directly in the current data object, before the next query operation, we can extract, such as to obtain the results of the query data:

PHP Code

1. $name = $User->name;

2. $email = $User->email;

Traverse query to data object properties

PHP Code

1. foreach ($User as $key => $val) {

2. Echo ($key. ': $val);

3.}

Or make related data changes and save operations

You can also use variables to keep them available.

PHP Code

1. $user = $User->find (8);

For the above query conditions, we can also use GetByID to complete the same query

PHP Code

1. $User->getbyid (8);

It is important to note that for the Find method, even if the query result has more than one record, only the first record that meets the criteria is returned, using the FindAll method if you want to return all records that meet the requirements.

PHP Code

1.//Query primary key for 1, 3, 8 recordset

2. $User->findall (' 1,3,8 ');

3.//Traverse Data list

4. foreach ($User as $vo) {

5. Dump ($vo->name);

6.}

For more queries, refer to the following chapters.

Update records

Once you know the query record, the update operation becomes very simple.

You can also use the following methods to update

PHP Code

1. $User->find (1); Find data with a primary key of 1

2. $User->name = ' Topthink '; Modifying Data Objects

3. $User->save (); Save the current data object

4. $User->score = ' (score+1) '; Add 1 to the user's points

5. $User->save ();

You can pass in data and conditions that you want to save if you are not saving it by using a data object

PHP Code

1. $data [' id '] = 1;

2. $data [' name '] = ' topthink ';

3.   $User->save ($data);  

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.