ThinkPHP study notes (8) in this section, we use database-based links and conf configurations based on 6 and 7, and mainly use the CURD method in action and updateaction & lt ;? PhpclassCurdActionextendsAc ThinkPHP study notes (8) related operations of CURD and associated query of tables
This section uses database-based connection and conf configuration based on 6 and 7.
Mainly the curd method in action and update
Action
Previously, the session and form had a // token that can be configured in the conf; you can set not to display the token on the page, but use the token the same way.
// If there are multiple forms on the page and only one form requires a token, you can specify the following in the form:
// By default, create obtains data by using the post method public function index () {$ this-> display ();} public function add () {$ user = new Model ('user'); // do not use the create method to verify the token // if (! $ User-> autoCheckToken ($ _ POST) {// code indicating the token verification failed; // $ this-> error ($ user-> getError ()); ///} else {// obtain the information $ user // $ user-> username; // $ user-> username = md5 ($ user-> password ); //// dump ($ user); // if ($ user-> add () {// $ this-> success ('added successfully '); ///} else {// $ this-> error ($ user-> getError (); //} // if the returned array information is automatically filled, if ($ vo = $ user-> create () {// This method is a method for successful execution, the page information is displayed. // dump ($ vo); // The successs template can be found in ThinkPHP example and placed in To the public directory in default // $ this-> success ('create successful '); // obtain the information $ user-> username = md5 ($ user-> password); // dump ($ user); if ($ user-> add ()) {$ this-> success ('added successfully');} else {$ this-> error ($ user-> getError ());}} else {// if the execution fails, the code will not continue to be executed backward // dump ($ vo); // dump ($ user ); // The error template can be found in ThinkPHP's example and placed in the public directory of default $ this-> error ($ user-> getError ());}} public function curd () {$ user = M ('user'); // find () query a single record // select () query all // FindAll () is the select method of the same name // $ find = $ user-> find (); // $ select = $ user-> select (); // $ findAll = $ user-> findAll (); // dump ($ find); // dump ($ select); // dump ($ findAll ); // 1: query involves a coherent operation problem; Manual: 5.3.10 // 1. you can use strings, arrays, and objects to pass values in the where query based on conditions. // functions include data, where, limit, order, field, group, and having, secondary filtering result set) and page (pageView is used for paging) // The data method can be used instead of the save method for transferring values, add, and delete. // $ select = $ user-> select (array ('where' => 'Id> 1 ', 'limit' => '2, 8', 'order' => 'Id desc'); // $ select = $ user-> where ('Id = 3')-> find (); // $ select = $ user-> where ('Id> 1')-> limit ('2')-> order ('Id desc ') -> field ('Id, username')-> select (); // 2. $ user-> table () is used to operate multiple tables; you can also use field to restrict query columns/$ select = $ user-> table (array ('TB _ user' => 'user ', 'TB _ user_message '=> 'm')-> where ('user. id = m. ID')-> select (); // $ select = $ user-> table ('TB _ user, tb_user_message m')-> where ('user. id = m. ID')-> select (); // 3. $ user-> join ()/ /$ Select = $ user-> join ('User on user. id = user_message.id ')-> select (); // 4. $ user-> distinct () // $ select = $ user-> distinct (true)-> select (); // 5. join model: $ user-> relation () // 6. lock: $ user-> lock () // You do not need to consider the lock mechanism (mysql5.0 myisam engine supports table-level locks and innodB engine supports row-level locks) // $ select = $ user-> lock (true)-> select (); // Two: getField () // $ select = $ user-> where ('Id> 1')-> getField ('Id, username'); // update operation: the returned value is the number of affected rows. // 1. in save (), you can directly input the data you want to change. Format: field information array // you can leave where, but the array Where condition that must be simulated // $ data ['id'] = '4'; // $ data ['username'] = 'aaa '; // $ data ['password'] = 'aaa'; // $ select = $ user-> where ('Id = 4')-> save ($ data ); // $ select = $ user-> where ('Id = 4')-> data ($ data); // 1. setField () parameter: Field, value // $ select = $ user-> where ('Id = 4')-> setField (array ('username', 'password '), array ('Google ', 'Google'); // 2. setInc (): adds and updates numeric fields. Parameters: Field, where, and added integer (plus one by default) // $ select = $ user-> setInc ('price', 'id = 2', 1 ); // 3. setDec () is used to reduce and update numeric fields. Parameters: Field, where, and integer (minus one by default) // $ select = $ user-> setDec ('price', 'id = 2', 1 ); // add operation: return the inserted id // $ data ['username'] = 'aaa'; // $ data ['password'] = 'aaa '; // $ select = $ user-> add ($ data); // $ select = $ user-> data ($ data)-> add (); // delete operation: number of affected rows // $ select = $ user-> delete (6); // $ select = $ user-> where ('Id> 4')-> delete (); // $ select = $ user-> where ('Id> 1')-> limit ('1')-> order ('Id desc')-> delete (); dump ($ select); // update the data of the specified id // $ this-> display ();} Public function update () {$ user = new Model ('user'); // if the returned array information is automatically filled in, if ($ vo = $ user-> create () {$ user-> username = md5 ($ user-> password ); if ($ user-> save () {$ this-> success ('added successfully ');} else {$ this-> error ($ user-> getError ());}} else {$ this-> error ($ user-> getError () ;}}}?>
Html
CURD