THINKPHP5 Operating Database code example

Source: Internet
Author: User
Tags save file

table structure CREATE table ' Qrcode_file ' (' id ' int () notNULLAuto_increment,' active ' int (1)DEFAULT' 0 ' COMMENT ' is effective ',' owner_id ' int (20)DEFAULT' 0 ' COMMENT ' owner ID ',' owner_role_id ' int (20)DEFAULT' 0 ' COMMENT ' Everyone role ID ',' qrcode_url ' varchar (500)DEFAULT"COMMENT ' two-dimensional code after parsing ',' scene_id ' int (10)DEFAULT' 0 ' COMMENT ' scene value ID ',' scene_str ' varchar (200)DEFAULT' COMMENT ' scene value ID ',' file_id ' int (20)DEFAULT' 0 ' COMMENT ' Save file ID ',' create_by ' varchar (25)DEFAULT‘‘,' update_by ' varchar (25)DEFAULT‘‘,PRIMARYKEY(' id ')) ENGINE=innodbDEFAULTCharset=utf8 comment= ' QR code file table '; Controller Code//=================================1. New data ================================////$newQrcodeFile = new Qrcodefile ();        Var_dump ($newQrcodeFile->get_data ());//The parameter is not returned to the data//$newQrcodeFile when instantiated->active=0;        $newQrcodeFile->owner_id=123321;        $newQrcodeFile->owner_role_id=2;        $newQrcodeFile->scene_id=123456;        $newQrcodeFile->scene_str= "Test";        $newQrcodeFile->create_by= "Lizhaoyao";        $newQrcodeFile->update_by= "Lizhaoyao"; Var_dump ($newQrcodeFile->get_data ());//member variable operation and return to data//var_dump ($newQrcodeFile->save ());//Call the Save method Saving the data only returns the success or not returning the self-increment ID//var_dump ($newQrcodeFile->getlastinsid ());//Call Getlastinsid to return the last self-increment ID//echo $newQ        Rcodefile->getlastsql ();        Exit        =================================2. Deleting data ================================////$newQrcodeFile = new Qrcodefile ();        $PK _id=9; 2.1 PRIMARY KEY Delete//$newQrcodeFile:: Get ($pk _id);//FindData to primary Key ID 9//This step is set to//protected ' Isupdate ' in the model ' = ' = Boolean true//protected ' Updatewhere ' =&gt        ; Array ("id" =>9)//$newQrcodeFile->delete (); 2.2 Destroy Delete can be bulk//var_dump ($newQrcodeFile::d Estroy ($pk _id));//Call destroy method directly delete//support bulk Delete multiple data//va R_dump ($newQrcodeFile::d Estroy (' 11,12,13 '));//delete data with ID 11 12 13 Bulk Delete returns 3 successfully deleted number of bars//var_dump ($newQrcodeFile::d estro Y (Array (21,22,23)));//delete data with ID 21 22 23 Bulk Delete returns 3 number of successfully deleted//2.3 PRIMARY key condition Delete//$delete _result= $newQrcodeFile->w        Here (' id ', ' > ', '->delete ');//delete the id>25 data//var_dump ($delete _result);//returns 2 successfully deleted//2.4 + Deleted Items        $delete _result= $newQrcodeFile->where (Array ("scene_id" = "123456", "owner_id" =>123))->delete ();        Var_dump ($delete _result);//returns 2 the number of successfully deleted bars//exit;        =================================3. Modifying data ================================////$newQrcodeFile = new Qrcodefile (); 3.1 First find the data and then update the numberIt is used as an object member variable for each operation//$user = $newQrcodeFile:: Get (20);//find data with ID 20//$user->owner_id = ' 789 ';//Assign Owner _id//$user->active = ' 1 ';//Assignment Active//var_dump ($user->save ());//The result of the update update is 0 or 1//3.2 directly through Array form Assignment update//$save _result= $newQrcodeFile->save (Array ("owner_id" = "951", "active" =>3), Array ("id" =>24)) ;//Update//var_dump ($save _result) by Save second parameter pass condition;//The result of the update is 0 or 1//3.3 call where to set condition Update//$update _ via the Update method        result= $newQrcodeFile->where (Array ("id" =>10)->update (Array (' scene_str ' = ' tp5 '));        Var_dump ($update _result);//The result of the update is 0 or 1//exit;        =================================4. Querying data ================================////$newQrcodeFile =new qrcodefile ();        4.1 Use the Get static function primary key to get/$info = $newQrcodeFile:: Get (20),//through the primary key lookup and then access the//var_dump ($info->id) through the member variable property;        Var_dump ($info->active);        Var_dump ($info->owner_id); Var_dump ($info->qrcodE_url);        Var_dump ($info->scene_id); 4.2 Use the Where condition with the Find method//$info = $newQrcodeFile->where (' owner_id ', ' 789 ')->find ();//Gets the object also needs to be accessed through the member variable properties        Take//var_dump ($info->id);        Var_dump ($info->active);        Var_dump ($info->owner_id);        Var_dump ($info->qrcode_url);        Var_dump ($info->scene_id); 4.3 Get multiple data with primary key//$all _list= $newQrcodeFile:: All ("20,24,25");//Gets the number of objects that can traverse and then get the member variable to get the field value//foreach ($all _l        ist as $info)//{//Var_dump ($info->id);        Var_dump ($info->active);        Var_dump ($info->owner_id);        Var_dump ($info->qrcode_url);        Var_dump ($info->scene_id); }//4.4 multiple//$all _list= with a Where condition select $newQrcodeFile->where ("owner_role_id", "2")->order ("id", "desc"        )->limit (2)->select ();        foreach ($all _list as $info)//{//Var_dump ($info->id); Var_dump ($info->active);        Var_dump ($info->owner_id);        Var_dump ($info->qrcode_url);        Var_dump ($info->scene_id);         //4.5 gets the value of a column//$column _value= $newQrcodeFile->where ("id")->value ("Scene_str");//gets directly a string or an integral type You can also use the column method//$column _value= $newQrcodeFile->where ("owner_role_id", "3")->column ("Scene_str", "id")        ;//acquired directly by an array of ID keys//var_dump ($column _value);        4.6 Questions come a lot of times we use the query results are objects and PHP developers like to use the array to do?        You can use the ToArray () method to turn an object into an array such as//$info = $newQrcodeFile:: Get ()->toarray (); $info = $newQrcodeFile->where (' owner_id ', ' 789 ')->find ()->toarray ()//If the table defines a time format field that is not a timestamp, the error indicates that the conversion time failed//        $all _list= $newQrcodeFile:: All ("20,24,25");//The data obtained by the list needs to be traversed and then toArray to the array//foreach ($all _list as $info)//{        $info = $info->toarray ();        Var_dump ($info); }//Use the All method to find out that the ToArray method can not be used to convert an array of//$all _list= $newQrcodeFile-&Gt;where ("owner_role_id", "2")->order ("id", "desc")->limit (2)->select ();        foreach ($all _list as $info)//{//$info = $info->toarray ();        Var_dump ($info); //}Model Code Qrcodefile.PHP<?php namespace App\index\model;  UseThink\model; classQrcodefileextendsmodel{//Custom Initialization        functionGet_data () {return $this-data; }    }

THINKPHP5 Operating Database code example

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.