Curd operations of ThinkPHP

Source: Internet
Author: User

ThinkPHP provides flexible and convenient data operation methods. It not only implements four basic operations (CURD) for database operations: creation, reading, updating, and deletion, there are also many built-in practical data operation methods that provide the best user experience in the ActiveRecords mode.


Create record


PHP code


1. $ User-> find (2 );


2. $ User-> delete (); // delete the found record


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


4. $ User-> deleteAll (); // Delete All queried data


PHP code


1. // instantiate a User model object


2.


3. $ User = new UserModel ();


4. // then assign values 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. // you can use


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


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


12. $ User = new UserModel ($ data );


13. $ User-> add ();


14. // or directly input the data to be created in the add Method


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


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


17. $ User = new UserModel ();


18. $ User-> add ($ data );


19.


20.


 


Generally, data objects in an application cannot be written by manually assigning values. Instead, a data object is created. ThinkPHP provides a create method to create data objects, and then perform other new or edit operations.


PHP code


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


2. $ User-> create (); // create a User data object. By default, the data submitted through the form is created.


3. $ User-> add (); // add data submitted by the form


The Create method allows you to Create data objects from other methods, such as other data objects or arrays.


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


 


Supports adding multiple records


PHP code


1. $ User = new UserModel ();


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


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


4. $ data [1] ['name'] = 'current year ';


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


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


Under the MySql database, an SQL statement is automatically used to insert multiple data.


Query records


Reading database records I think is the most interesting thing in database operations. Anyone who has written a Text Database knows that it is not difficult to store and delete data (it is nothing more than a matter of standardization and efficiency ), it is difficult to find the required data in various ways. Through various efforts, ThinkPHP makes database query operations easy and makes ThinkPHP rich.


ThinkPHP has a very clear convention that individual data queries and multiple data query methods are separated, or you may think that sometimes you do not know whether the data to be queried is single or multiple, but it is clear that you need to return a data or want to return a data set. Because the operation methods for two types of returned data are completely different, regardless of the Return method, we can directly operate in the model object, of course, it can also be used as a variable to pass data to you.


First, let's give a simple example. If we want to query a user record with a primary key of 8, we can use 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 saved in the current data object. We can extract the results before the next query operation, for example, obtain the query result data:


PHP code


1. $ name = $ User-> name;


2. $ email = $ User-> email;


Traverse the queried Data Object Attributes


PHP code


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


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


3 .}


// Or modify and save the data.


You can also save it as a variable for use at any time.


PHP code


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


For the preceding query conditions, we can also use getById to complete the same query.


PHP code


1. $ User-> getById (8 );


It should be noted that for the find method, even if there are multiple records in the query results, only the first record meeting the conditions will be returned. If you want to return all the records meeting the requirements, use the findAll method.


PHP code


1. // query Record Sets whose primary keys are 1, 3, and 8.


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


3. // traverse the data list


4. foreach ($ User as $ vo ){


5. dump ($ vo-> name );


6 .}


For more query operations, see the following sections.



Update record


After learning about the query records, the update operation is very simple.


// You can also use the following method to update


PHP code


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


2. $ User-> name = 'topthink'; // modify the Data Object


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


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


5. $ User-> save ();


If the data object is not used for storage, you can pass in the data and conditions to be saved.


PHP code


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


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


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


In addition to the save method, you can also use the setField method to update the value of a specific field, for example:


PHP code


1. $ User-> setField ('name', 'topthink', 'Id = 1 ');


Field operations are also supported.


PHP code


1. $ User-> setField ('score ',' (score + 1) ', 'Id = 1 ');


2. // or change it to the following


3. $ User-> setInc ('score ', 'Id = 1 ');


Delete record


If your primary key is of the auto-increment type, you can create a new data without passing in the value of the primary key. If the data is successfully inserted, the return value of the Add method is the newly inserted primary key value, can be obtained directly.

 


Echo $ user-> getLastSql ();

 

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.