ThinkPHP Study Notes 1-CURD

Source: Internet
Author: User
ThinkPHP Study Notes 1 ----- CURDThinkPHP provides us with a very quick way to perform the curd operation. how to use it or read the manual? here I mainly write a bit of my own experience, for example (1) Access: http: // localhost: 1243/think/index. php/Form/show & nbsp; Display data ThinkPHP Study Notes 1--CURD

ThinkPHP provides us with a very quick way to execute the curd operation. how to use it or read the manual? here I mainly write a little bit of my experience.

For example

(1) Access: http: // localhost: 1243/think/index. php/Form/show to display database list functions

The code is as follows:

Public function show () {// output table $ user = M ("user"); $ userArr = $ user-> select (); // dump ($ userArr ); $ this-> assign ("userArr", $ userArr); $ this-> display ();}

1. in the M () function, user indicates the table name, that is, the user table in the database. if the object cannot be instantiated, check:

(1) whether 'DB _ DSN 'and 'DB _ prefix' are set in conf/config. php (if the table name has a PREFIX );

(2) whether the table prefix _ user table exists

2. display (0 is a function in the Action class, just like Smarty, ThinkPHP's Action is equivalent to the $ smarty class instantiated in smarty. if the display page is not specified, tpl/Form/show.html is searched by default.


(2). access: http: // localhost: 1243/think/index. php/Form/add new data to the database

The code is as follows:

// Add public function add () {// $ model = D ("Form"); $ user = M ("user "); // The Verification rule can be written in action $ validate = array ('name', 'require ', 'user name cannot be blank '), // empty verification array ('name', '', 'username already exists ', '1', 'Unique', '1'), array ('mail ', 'email ', 'Email format error'),); // write the rule to model $ user-> setProperty ("_ validate", $ validate );
If (! Isset ($ _ POST ['sub']) {$ this-> display (); exit ;} // form submission // unset ($ _ POST ['sub']); // The create FUNCTION verifies the database, some columns in the array will exist in the array returned by create, so you can save the unset operation // for example, in the database table, there is no column mail, but the form is submitted with mail, the returned array after the create statement is verified does not contain mail $ res = $ user-> create (); if (! $ Res) {// error message $ error = $ user-> getError (); $ this-> assign ("error", $ error ); $ this-> display (); exit;} $ result = $ user-> add (); if ($ result> 0) {$ this-> success ("added successfully", "_ URL _/show");} else {$ this-> error ("failed to add, please try again ");}}

(1 ). D () functions cannot be set separately after 3.1. if a FormModel is defined under the Model folder. class. php, and then instantiate the Model with D ("Form"). The system will prompt that the data table Form does not exist.

No method found. please kindly advise

(2) when performing automatic verification, you can write the verification rules in the action.

If you want to write it in a custom Model

Protected $ _ validate = array (// verification rules can be written in the action array ('name', 'require ', 'user name cannot be blank '), // empty verification array ('name', '', 'username already exists ', '1', 'Unique', '1'), array ('mail ', 'email ', 'Email format error '),);

(3) for details about the create and add functions, see the comments in the code above.


You are welcome to raise questions and solve them together.

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.