thinkphp Curd Operation _php Tutorial

Source: Internet
Author: User
Thinkphp provides a flexible and convenient method of data manipulation, not only to achieve 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, provides the best experience of activerecords mode.


New record


PHP Code


1. $User->find (2);


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


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


4. $User->deleteall (); Delete all data from the 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 a lock is required to instantiate a model object when the data is passed in, you can use


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


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


$User = new Usermodel ($data);


$User->add ();


14.//or simply pass in the Add method to the newly created data


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


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


$User = new Usermodel ();


$User->add ($data);


19.


20.



In general, data objects in an app are not likely to be written by manual assignment, but rather have a data object creation process. Thinkphp provides a create method for creating data objects, and then other additions or edits.


PHP Code


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


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


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


The Create method supports the creation of data objects in other ways, such as 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 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 ';


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


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


Under the MySQL database, multiple data insertions are automatically implemented using an SQL statement.


Query records


Reading records from a database I think it's the most interesting thing in database operation, people who have written a text database know that saving and deleting data is not difficult (nothing more than specification and efficiency), it is difficult to find the data you need in a variety of ways. Thinkphp through various efforts to make the database query operation becomes easy, also let thinkphp become rich connotation.


Thinkphp has a very clear agreement 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. Since the operation of the two types of return data is very different, regardless of the way the return, we can directly inside the model object operation, of course, as well as the data can be passed to the variables you need.


Let's start with the simplest example, if we want to query a user record with a primary key of 8, we can use some of the following methods:


PHP Code


1. $User->find (8);


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


PHP Code


1. $name = $User->name;


2. $email = $User->email;


Iterating through the data object properties of a query


PHP Code


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


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


3.}


Or make relevant data changes and save operations


Variables can also be saved for use at any time.


PHP Code


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


For the above query criteria, 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 results have more than one record, only the first record that meets the criteria is returned, and if you want to return all records that meet the requirements, use the FindAll method.


PHP Code


1.//Query the recordset with primary key 1, 3, 8


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


3.//Traverse Data list


4. foreach ($User as $vo) {


5. Dump ($vo->name);


6.}


For more query operations, refer to the following sections.



Update record


Once you understand 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 '; Modifying Data Objects


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


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


5. $User->save ();


If you are not saving by using a data object, you can pass in the data and conditions you want to save


PHP Code


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


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


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


In addition to the Save method, you can use the SetField method to update the value of a specific field, for example:


PHP Code


1. $User->setfield (' name ', ' Topthink ', ' id=1 ');


You can also support operations on fields


PHP Code


1. $User->setfield (' Score ', ' (score+1) ', ' id=1 ');


2.//or change to the following


3. $User->setinc (' score ', ' id=1 ');


Deleting records


If your primary key is an autogrow type, you can create new data without having to pass in the value of the primary key, and if the data is inserted successfully, the return value of the Add method is the most recently inserted primary key value, which can be obtained directly.


echo $user->getlastsql ();

http://www.bkjia.com/PHPjc/477477.html www.bkjia.com true http://www.bkjia.com/PHPjc/477477.html techarticle thinkphp provides a flexible and convenient method of data manipulation, not only to achieve the four basic operations of database operations (CURD): Create, read, update and delete the implementation, but also built a very ...

  • Related Article

    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.