Home  >  Article  >  Backend Development  >  Curd operation of ThinkPHP_PHP tutorial

Curd operation of ThinkPHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:10:44986browse

ThinkPHP provides flexible and convenient data operation methods. It not only implements the four basic operations of database operations (CURD): create, read, update and delete, but also has many practical data operation methods built-in. Provides the best experience in ActiveRecords mode.


Create new record


PHP code


1. $User->find(2);


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


3. $User->delete('5,6'); // Delete data with primary keys 5 and 6


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


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 data when instantiating the model object, you can use


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


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


"

13. $User->add();

14.                   // Or directly pass in the new 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.                                            



Under normal circumstances, data objects in applications are unlikely to be written through manual assignment, but there is a data object creation process. ThinkPHP provides a create method to create data objects, and then perform other adding or editing operations.


PHP code


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


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


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


The Create method supports creating data objects in 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 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'] = 'Fleeting Years';


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


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


Under the MySql database, one SQL statement will be automatically used to insert multiple data.


Query records


I think reading database records is the most interesting thing in database operations. Anyone who has written a text database knows that it is not difficult to save and delete data (it is nothing more than a matter of standardization and efficiency). The difficulty is that it can be done through various way to find the data you need. Through various efforts, ThinkPHP makes database query operations easy and makes ThinkPHP rich in content.


ThinkPHP has a very clear convention, that is, the methods of single data query and multiple data query are separate, or you may think that sometimes you don’t know whether the data you want to query is single or multiple, but one thing is clear, Do you need to return a piece of data or do you want a data set to be returned? Because the operation methods for the two types of returned data are completely different, no matter which method is returned, we can operate directly in the model object, and of course it can also be passed as data to the variables you need.


Let’s take the simplest example first. If we want to query a user record whose primary key is 8, we can use the following methods:


PHP code


1. $User->find(8);


This is the most intuitive as a query language. If the query is successful, the query results are directly stored in the current data object. We can extract them before the next query operation, such as obtaining 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 perform related data changes and save operations


You can also use variables to save them for use at any time.


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 should be noted that for the find method, even if the query result has multiple records, only the first record that meets the conditions will be returned. If you want to return all records that meet the requirements, please use the findAll method.


PHP code


1. // Query the record set whose primary key is 1, 3, 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, please refer to the following chapters.


                           


Update record


After understanding the query record, the update operation is very simple.


// You can also use the following method to update


PHP code


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


2. $User->name = 'TOPThink'; // Modify 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 you do not use data objects to save, 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');


Operations on fields can also be 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 an automatic growth type, you can create new data without passing in the value of the primary key. And if the data is inserted successfully, the return value of the Add method is the latest inserted primary key value, which can be obtained directly.


echo $user->getLastSql();

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477477.htmlTechArticleThinkPHP provides flexible and convenient data operation methods, not only realizing the four basic operations of database operations (CURD ): Implementation of creation, reading, updating and deletion, and many built-in...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn