Application of thinkphp5.0 Model

Source: Internet
Author: User

            $where=Array();//            $where [' show_id '] = 1;//var_dump (Db::connect ());//view database configuration//$res = Db::query (' Selec T * from fanke_show where show_id =? ', [1]);//query with SQL statement//$res = Db::name (' Show ', [],false], where ($where ), select ();//The prefix is already entered in the database, and the third parameter is false, then it is not instantiated every time the call is made//$res = Db::execute (' INSERT INTO fanke_show se T show_id=?,show_image_scr=?,show_title=?,show_content=?,show_time=? ',///' 2 ',//' http://www.qq.com ',//' Yes! Is you ',//' You are very handsome! ",//22//]);//Use SQL statement to insert data//$res = db::table (' fanke_show '), wher  E ($where), select ();//select returns all records returned worth the result is a two-dimensional array//$res = db::table (' fanke_show '), where ($where) --Find ();//find returns a record that returns a one-dimensional array if the result does not exist, returns null//$res = db::table (' fanke_show '), where ($whe RE), value (' Show_title ');//value returns theA record is returned null//$res = db::table (' fanke_show '), where a field value does not exist, and where ($where), column (' Show_titl E ', ' show_title ');//column returns the value of the one-dimensional array, which is the value of the column we want to get, and returns null//----------------------------------------                Add data---------------------------------------------------------//$db = Db::name (' show ');//This is the way I didn't think of it before. Insert Insert data put back the number of rows inserted//insertgetid insert data returns the number of rows inserted and the increment ID//insertall inserts more than one data at a time, returns Number of rows successfully inserted//$where = null;//$where [' show_image_scr '] = ' you ';//$where [ ' show_title '] = ' very ';//$where [' show_content '] = ' handsome ';//$where [' show_time '] = 333;/// /$res = insert ($where) $db,//Insert data into the number of rows inserted//$res = $db-Insertgetid ($where);//                        Insert data returns the number of rows inserted and the increment id//$data = [];//for ($i = 0; $i <9; $i + +) {//            $data [$i] = $where;//         }//$res = $db-Insertall ($data); Insert more than one data at a time, returning the number of rows successfully inserted//--------------------------------            Database update----------------------------------------------------------//$db = Db::name (' show '); Update updates one data return is the number of rows//setfield only one field is updated//setinc The default value is 1, and the number of rows affected is returned//SETDEC self-reduction defaults is 1, returns the number of rows affected//update//$res = $db->where ([//' show_id ' + 1//])                -Update ([//' show_image_scr ' = ' 123 ',//' show_title ' = ' 123 ',//            ' show_content ' = ' 123 ',//' show_time ' = ' 123 '//];                setfield//$res = $db, where ([//' show_id ' = 5//]), SetField (//              ' Show_title ', ' Who am I? '); setinc//$res = $db->where ([//' show_id ' = 8//])->setinc (' Show_time ', 5)            ; //setdec//$res = $db->where ([//' show_id ' = 8//])->setdec (' Show_time ', 5);  /--------------------------------Data deletion----------------------------------------------------------//delete Delete a piece of data             Returns the number of rows affected if your condition is the primary key, you can pass the data directly in the Delete () method//$res = $db, where ([//' show_id ' = + 10// ])->delete ()//$res = $db->delete (one),//$res = $db, where ([' 1=1 ']), delet E ();//delete all data//--------------------------------conditional constructor---------------------------------------------------------//            BUILDSQL return SQL OUTPUT statement finally see conditional constructs video//eq =//neq <>//gt >//get >= Between Betweew * and * between//notbetween Notbetween *and* not between this//in in (*,*)/                /notin not IN (*,*)//$res = $db, where ([//' show_id ' = = [' Get ', [1,10]],//and// ' Show_title ' =&Gt            ' Very '//]), BuildSQL ();//--------------------------------chain Operation---------------------------------------------                Look back at the video.//field//order//limit//page//$res = $db// --Where (' show_time ', ' eq ', ' 333 ')//----(' show_title ')//-----* (4,5            //-select ();//----------------------------------Model----------------------------------------------              Get the model//preferably use the first two methods, so that the readability is higher//$res = S::get (3);//The first//$show = The second type of new s;//// $res = $show:: Get (1),//$show = Loader::model (' show '),///The third method above two to use their model, this does not need, as long as using Think\loa Der, other common, applicable to multiple models//$res = $show:: Get (1);//$show = Model ("show"); This doesn't use anything.//$res = $show:: Get (6);//-------------------------------model query data---------------------------------         -----------------//Query a single data//     $res = S::get (2);//Var_dump ($res, Show_title)//Query single data//$res = S::get (function ($query) {//$query where (' show_id ', ' eq ', ' 5 ')//, field (' Show _id '//});//Query single data//$res = S::where (' show_id ', 2)//->fie LD (' show_id ')//->find ()//Query multiple data//$where = Array ();//$where [' s               how_id '] = 2;//$res = S::all ($where);//foreach ($res as $k = + $v)//{// $v = $v ToArray ();//Var_dump ($V),//}//query multiple data//$res = S::all (fu            Nction ($query) {/////This adds conditions, without select and find//$query, where (' show_id ', ' < ', 5);//                });//foreach ($res as $k = + $v)//{//$v = $v-ToArray ();//     Var_dump ($v);//       }//Query multiple data//$res = S::where (' show_id ', ' > ', 2)//->field (' show_id ')  ->select ();//foreach ($res as $k + = $v)//{//$v = $v ToArray ()///Var_dump ($V)//}////$res = S::where ([' show_id ' =&            GT;1])->value (' Show_title '); The second parameter to get multiple fields of data is to set subscript key//$res = s::column (' show_content ', "show_title");//-----------------------Add data-------            -------------------//Method one adds a single data//$where = Array ();//$where [' show_title '] = ' Who am I ';// $where [' show_content '] = ' where am I ';//$where [' show_time '] = 123456789;//$res = s::create ($wh  Ere,true);//true will not automatically exclude fields from the database/if [' Show_title ', ' show_content '] he will only add data for these two fields//method two add a single data//$show =      New s;//$show-show_title = ' come ';//$show, show_content = ' this ';//      $show-Show_time = 123;//$res = $show, save (); Method three adds a single data//$show = new s;////allowfield () will not be automatically excluded from the database field/if it is [' show_title ', ' show_content '] he will only add this            Two fields of data//$res = $show, Allowfield (True), Save ($where); Add multiple data to return model object two-dimensional array//$show = new s;//$res = $show, SaveAll ([//[' Show_title                 ' = ' i haha '],//[' show_title ' = ' Pepsi ']//]);//foreach ($res as $k = + $v) {// Var_dump ($v->toarray ());//}//------------------------Update Data--------------------------------------                ----//Just update some simple data this way, because the return value of the update two times is the same, it is not possible to determine whether the update//As long as there is a primary key, there is no need to write the condition//$res = S::update ([// ' show_id ' = ' + ',//' show_title ' = ' 6666 ',//' show_content ' and ' 7777 7 ',//' show_time ' = ' 8888 '/]);//$res = S::update ([' Show_title ' = ' 6666 ',//' show_content ' = ' 77777 ',//' show_time ' = > ' 8888 '//],[' show_id ' =>5]);//$res = S::update ([//' show_title ' = ' 6666 ', ' Show_content ' = ' 77777 ',//' show_time ' = ' 8888 '//],function ($query) {            $query, where (' show_id ', ' < ', 5);/});                    Update two updates will return the number of rows affected, otherwise return 0//$res = S::where (' show_id ', ' eq ', 5)//-Update ([//            ' Show_title ' = ' I am the method two '//]);            Update three updates will return the number of rows affected, otherwise return 0//$model = s::get (1);//Get Data Model//$model, Show_title = ' 123 ';//change value// $res = $model Save ();//auto update//update four The second parameter is used for selection//$show = new s;//$res = $   Show, Save ([//"show_title" = ' Your sister's '//],[//' show_id ' = 1//             ]//                ); Update five The second parameter is used for selection//$show = new s;//$res = $show, save ([//"Show_title" = "You                Sister's '//],function ($query) {//$query, where (' show_id ', ' < ', 3);//}// );//----------------------------Delete data----------------------------------------//Method One//$res = s::d            Estroy (1);//Method two//$res = s::d estroy ([' show_id ' = 2]);            Method three-batch deletion//$res = s::d Estroy (function ($query) {//$query, where ("show_id", ' > ', 5);//            });//Method four//$model = S::get (5);//$res = $model, delete (); Method Five            $res= S::where (' show_id ', 5)Delete ();//$res = $res-ToArray ();                Var_dump($res);

Application of thinkphp5.0 Model

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.