Curd operation of ThinkPHP_PHP tutorial
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.
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();

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool