Notes for small Qthinkphp

Source: Internet
Author: User
CURD refers to the combination of four database operations, namely "Create, Update, Read, Delete (Create, modify, Read, Delete)". It is also described in detail in the development guide. thinkPHP provides flexible and convenient data operation methods, not only for database operations.

CURD refers to the combination of four database operations, namely "Create, Update, Read, Delete (Create, modify, Read, Delete)". It is also described in detail in the development guide.

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

The Model class unifies database operations as CURD and an SQL query method, that is

_ Create add (write) data

_ Update (save) data

_ Read (query) data

_ Delete: delete data

_ Query SQL query

All other methods basically operate on the basic methods you call, but we do not need to pay attention to these details, but only need to follow the provided abstract methods, let's take a look at how ThinkPHP performs data operations.

1. create a record

  1. // Instantiate a User model object
  2. $ User = new UserModel ();
  3. // Assign values to the data object
  4. $ User-> name = 'thinkphp ';
  5. $ User-> email = 'thinkphp @ gmail.com ';
  6. // Then you can save the new User object.
  7. $ User-> add ();
  8.  
  9. // You can use
  10. $ Data ['name'] = 'thinkphp ';
  11. $ Data ['email '] = 'thinkphp @ gmail.com ';
  12. $ User = new UserModel ($ data );
  13. $ User-> add ();
  14.  
  15. // Or directly input the data to be created in the add method
  16. $ Data ['name'] = 'thinkphp ';
  17. $ Data ['email '] = 'thinkphp @ gmail.com ';
  18. $ User = new UserModel ();
  19. $ User-> add ($ data );

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 directly obtained.

$ InsertId = $ User-> add ($ data );

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.

  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
  4. The Create method allows you to Create data objects from other methods, such as other data objects or arrays.
  5. $ Data ['name'] = 'thinkphp ';
  6. $ Data ['email '] = 'thinkphp @ gmail.com ';
  7. $ User-> create ($ data );
  8.  
  9. // Create a new Member data object from the User data object
  10. $ Member = D ("Member ");
  11. $ Member-> create ($ User );
  12. Supports adding multiple records
  13.  
  14. $ User = new UserModel ();
  15. $ Data [0] ['name'] = 'thinkphp ';
  16. $ Data [0] ['email '] = 'thinkphp @ gmail.com ';
  17. $ Data [1] ['name'] = 'current year ';
  18. $ Data [1] ['email '] = 'liu21st @ gmail.com ';
  19. $ User-> addAll ($ data );

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

2. 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. ThinkPHP makes database query operations easy through various efforts and ThinkPHP becomes full of meaning.

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 passed as data to the variables you need.

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:

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

$ Name = $ User-> name;

$ Email = $ User-> email;

Traverse the queried data object attributes

  1. Foreach ($ User as $ key => $ val ){
  2. Echo ($ key. ':'. $ val );
  3. }
  4. // Or modify and save the data.

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

$ User = $ User-> find (8 );

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

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

  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. }

3. update records

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

  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. // You can also use the following method to update
  5. $ User-> score = '(score + 1)'; // add 1 to the User's points
  6. $ User-> save ();

If you do not want to save data objects, you can pass in the data and conditions to save.

  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:

$ User-> setField ("name", "TopThink", 'id = 1 ');

Field operations are also supported.

  1. $ User-> setField ("score", "(score + 1)", 'id = 1 ');
  2. // Or change it to the following
  3. $ User-> setInc ("score", 'id = 1 ');

4. delete records

  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

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.