Curd operation [1]

Source: Internet
Author: User

Create creates new data

First create the index.html in the home directory, with the index.php sibling, insert the following code

<MetaCharSet= "Utf-8"><formMethod= "POST"Action= "Http://localhost/demo39/index.php/Home/User/create">  <P>User:<inputtype= "text"name= "User" /></P>  <P>Mailbox:<inputtype= "text"name= "Email" /></P>  <P>Birthday:<inputtype= "text"name= "Birthday" /></P>  <inputtype= "Submit"value= "Submit"></form>

Then insert the following code in home/controller/usercontroller.class.php

<? PHP // This class is automatically generated by the system and is intended for testing purposes only namespace Home\controller;  Use Think\controller;  Use Think\model; class extends Controller {        publicfunction  Create () {            $user = M (' User ');             Var_dump ($user,Create ());}        }

Because the structure of the data table is:

So when you click Send in index.php, Var_dump () only the user and email data,

In other words, when you create a form in index.html, the name you set should correspond to the name in the datasheet.

classUsercontrollerextendsController { Public functionCreate () {$user= M (' User '); $data[' User ']= ' Xiao Li '; $data[' Email ']= ' [email protected] '; $data[' Date ']=Date(' y-m-d h:i:s '); Var_dump($user->create ($data)); }}

Then the var_dump data is the data $data set

classUsercontrollerextendsController { Public functionCreate () {$user= M (' User '); $data=New\stdclass (); $data->user =$_post[' User ']; $data->email =$_post[' Email ']; $data-Date=Date(' y-m-d h:i:s '); Var_dump($user->create ($data)); }}

The data for Var_dump () is the value of user and email submitted by index.html, plus date.

Described above is the method of post, that is, index.html in the form of method= "POST" , if you change to method= "get" , then

In Home/controller/usercontroller. the code in class. PHP is

class extends Controller {        publicfunction  Create () {            $user = M (' user ');             var_dump($user->create ($_get));}        }

That is, change the mode in Create to $_get to Var_dump () out the received data

The Create () method can pass the second parameter, and there are two types of modes that will be manipulated: Model::model_insert
And Model::model_update, that is, new and modified. When not specified, the system will be based on whether the data source is wrapped
With the primary key to automatically determine, if the primary key is included, it is the modification operation.

That is

$user = M (' user '); Var_dump ($user->create ($_post, Model:: Model_insert));

The internal work of the Create () method is divided into 9 steps:
1. Get the data source (default is POST);
2. Verify the legitimacy of the data (non-data or objects are filtered), and fail to return false;
3. Check the field mappings;
4. Determine the status of the data (new or modified);
5. The data is automatically verified, and the failure returns false;
6. Form token authentication, failure returns false;
7. Form data Assignment (filter illegal fields and string processing);
8. Automatic data completion;
9. Generate Data Objects (saved in memory).


The Create () method can be created with consistent operations with data, and the supported coherent operations are:
1.field, used to define a valid field;
2.validate, for automatic data validation;
3.auto for data auto-completion;
4.token for token authentication.

Limit the fields that can be manipulated
$user = M (' user '); Var_dump ($user->field (' user ')->create ());

Only the data received by the user is inserted into the database

The second method is in Home/model/usermodel. insert in class. PHP :

class extends  protected$insertFields = ' user 'protected$updateFields = ' user ';}

And then in Home/controller/usercontroller. Class. PHP Insert

$user = D (' user '); Var_dump ($user->create ($data));

Note that the D (' User ') is used here;

Why do you use the D method? Because the business logic is defined in the Custom model class (under the Lib/model directory), and you want to implement these business logic in the operation, you need to use the D method

For a method of D and M, you can view this article

Add Data Write

in  home/controller/usercontroller. Class . php   Insert the following code in

 Public function Add () {            $user=m (' user ');             $data [' User ']= ' Xiao Li ';             $data [' Email ']= ' [email protected] ';             $data [' Date ']=date(' y-m-d h:i:s ');             $user->add ($data);        }

Combine the Create () method

$user = M (' user '); $data $user, create (); $data Date (' y-m-d h:i:s '); $user->add ($data);

Submit the form in the originally written index.html, and the action submitted should be http://Localhost/demo39/index.php/home/user/add

Because if $user->add () is used directly, and not added

$data $user, create (); $data Date (' y-m-d h:i:s ');

The data submitted to the database is now out of time because the time data is submitted when the form is submitted.

The Add () method supports coherent operations such as:
1.table, define the data table name;
2.data, specifying the data object to write to;
3.field, define the field to write to;
4.relation, correlated query;
5.validate, automatic data verification;
6.auto, data automatic completion;
7.filter, data filtering;
8.scope*, naming range;
9.bind, data binding operation;
10.token, token authentication;
11.comment,sql notes;

Using the Data coherence method

$user = M (' user '); $data $user, create (); $data Date (' y-m-d h:i:s '); $user->data ($data)->add ();

The data coherence method supports strings, arrays, objects

$user = M (' user '); $data = ' user= star vector &[email protected]&date= '. Date (' y-m-d h:i:s '); $user->data ($data)->add ();

Curd operation [1]

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.