Go Summary of Thinkphp's curd easy to ignore

Source: Internet
Author: User

Turn from: http://www.oschina.net/code/snippet_2285640_44437,

1. Use object method to insert data D usage. $Form = D (' form '), $data [' title '] = ' thinkphp '; $data [' content '] = ' form contents '; $Form->add ($data); In fact, thinkphp also supports the object to insert data directly into the database, as follows: $Form = D (' form '), $Form->title = ' thinkphp '; $Form->content = ' form content '; $Form Add (); 2, do not specify conditions for data updates. $Form = M ("Form"); The data object property to be modified is assigned the value $data[' id ' = 5; $data [' title '] = ' thinkphp '; $data [' content '] = ' ThinkPHP3.1 version released '; $Form->save ($data) ; Saving modified data According to conditions the Save method automatically recognizes the primary key field in the data object and acts as an update condition. Of course, you can also explicitly pass in the update condition, which is our most commonly used method: $Form = M ("Form");//The Data object property to be modified $data[' title ' = ' thinkphp '; $data [' content '] = ' ThinkPHP3.1 release '; $Form->where (' id=5 ')->save ($data); Saving the modified data according to the condition actually has the way of the object, as mentioned above, insert the data, the same way as the object: $Form = M ("Form");//The Data object property assignment to be modified $form->title = ' thinkphp '; $Form- >content = ' ThinkPHP3.1 version released '; $Form->where (' id=5 ')->save (); The modified data is saved according to the condition and the primary key field can be contained in the data to be saved, so that you can write where $form = M ("Form"), and/or the data object property to be modified $form->id = 5; $Form->title = ' thinkphp '; $Form->content = ' ThinkPHP3.1 release '; $Form->save (); Saves modified data 3, modification of a single field value, based on the primary key in the data object. There are times when IYou only need to modify the value of a field, you can use the SetField method without having to call the Save method every time. $Form = M ("Form"); Change the title Value $form->where (' id=5 ')->setfield (' title ', ' thinkphp '); 6. Strong Increment/Decrement field value operation. Thinkphp, you can perform a direct or decrement operation on a field's data. For statistical fields, the system also provides a more convenient method for Setinc and Setdec. $User = M ("User"); Instantiate the User object $user->where (' id=5 ')->setinc (' Score ', 3); The user's points plus 3$user->where (' id=5 ')->setinc (' score '); User's points plus 1$user->where (' id=5 ')->setdec (' Score ', 5); User's points minus 5$user->where (' id=5 ')->setdec (' score '); The user's points are reduced by 17 and the where is not used to perform the deletion. $User->delete (' 1,2,5 '); Delete the user data for primary keys 1, 2, and 5. This usage can be used in an example of an auto-increment of user points, similar to the sign-in time. 8. Print SQL statement echo $User->getlastsql (), or Echo $User->_sql (); loop output volist also alias iterate template assignment: $User = D (' User ') $list = $ User->findall () $this->assign (' list ', $list) template definition: <iterate name= "list" id= "Vo" >{$vo. Name}</iterate > Note the meaning of name and ID//Output list 5th to 15th record <iterate name= "list" id= "Vo" offset= "5" length= ' >{$vo. Name}</itera te>//output even record <iterate name= "list" id= "Vo" mod= "2" ><eq name= "mod" value= "1" >{$vo. Name}</eq></iterate>//output key<iterate name= "list" id= "Vo" key= "K" >{$k}. {$vo. name}</iterate>//sub-loop output <volist name= "list" id= "VO" ><iterate name= "vo[' Sub ']" id= "sub" >{$ Sub.name}</iterate></volist>switch label <switch name= "name" ><case value= "1" >value1</case ><case value= "2" >value2</case><default/>default</switch> where the Name property can use functions and system variables, such as:< Switch Name= "think.get.userid|abs" ><case value= "1" >admin</case><default/>default</switch > can also use variables for the Value property of case, for example: <switch name= "userId" ><case value= "$adminId" >admin</case><case Value= "$memberId" >member</case><default/>default</switch> comparison label <eq name= "name" value= " Value ">value</eq>//Name variable values equal to value output <neq name=" name "value=" value ">value</neq>//Name The value of the variable is not equal to value output <gt name= "name" value= "5" >value</gt>//The value of the name variable is greater than 5 for output <egt name= "name" value= "5" >va LueThe value of the </egt>//name variable is greater than or equal to 5 for output <lt name= "name" value= "5" >value</lt>//The value of the name variable is less than 5 for output <elt name= "name  "Value=" 5 ">value</elt>//The value of the name variable is less than or equal to 5 output//actually all of the above labels are aliases of the compare tag//Where the value of the type attribute is the name of the judgment tag listed above <compare Name= "Name" value= "5" type= "EQ" >value</compare>//The value of the name variable equals 5 to Output if label <if condition= "$name eq 1" > Value 1<elseif condition= "$name eq 2"/>value2<else/> value3</if>c operation operation (dynamic) configuration: Mainly used in the action method inside get: C (' config parameter ') Settings: C (' config parameter ', new value) A action quickly creates the action object: $action = A (' user '), equivalent to $action = new Useraction ();D operation to quickly create A model data object: $model = D (' user '); $model = new Usermodel (); S Operation quick Action cache method gets: s (' name ') Set: S (' name ', ' value '); Delete: s (' name ', NULL); F Operation Fast File data save method Use the same method as the S operation L Operation fast Action language variable get: L (' language variable '); Set: L (' language variable ', ' value '), such as: L (' user_info ', ' user Information '); Set the batch assignment of a language variable named user_info: $arr [' language variable 1 '] = ' value 1 '; $arr [' Language variable 2 '] = ' value 2 '; L ($arr); thinkphp System Constants Think_path//thinkphp system directory App_path//current project directory App_name//current project name Module_name//Current module name action_name//current operation name Tmpl _path//project Template catalogue Lib_path// Project Class Library Directory Cache_path//project template Cache directory Config_path//Project Profile directory Log_path//project log file directory Lang_path//Project Language file directory Temp_path//Project temp file directory Plugin_ PATH//project plug-in file directory Vendor_path//third-party class library Directory Data_path//project Data file directory Is_apache//Whether it belongs to Apacheis_iis//whether it belongs to Iisis_win//Whether it belongs to Windows ring Is_linux//Whether it belongs to the LINUX environment IS_FREEBSD//Whether it belongs to the FREEBSD environment Now_time//Current timestamp memory_limit_on//whether there is memory usage limit memory_limit_on/whether there is memory Use the limit output_gzip_on//whether to turn on output compression MAGIC_QUOTES_GPC//magic_quotes_gpcthink_version//thinkphp version number Lang_set// Browser language template_name//current template name Template_path//Current template path __root__//website root address __app__//current project (entry file) address __url__//Current module address __action__ Current action address __self__//current URL address tmpl_file_name//The default template name for the current operation (with path) Web_public_url//Site Common directory App_public_url//project Common template directory predefined constants WEB _log_error=0//error log type web_log_debug=1//debug Log type sql_log_debug=2//SQL log type system_log=0//System mode logging mail_log=1//message logging T cp_log=2///TCP log file_log=3///File mode log data_type_obj=1//object mode return data_type_array=0//array return url_common=0//Normal mode Urlur L_pathinfo=1//PATHINFO urlurl_rewrite=2//RewritE urlhas_one=1//Has_one Association definition belongs_to=2//belongs_to Association definition has_many=3//has_many Association definition many_to_many=4//Many_to_many Association Righteousness exists_vailidate = 0//The form exists field is verified must_validate = 1//must verify Value_vailidate = 2//The form value is not empty then verify I method Echo I (' get.id '); Equivalent to $_get[' ID '] echo I (' get.name '); Equivalent to $_get[' name '] echo I (' get.name ', ' ', ' htmlspecialchars '); $_get[' name '] is filtered using the Htmlspecialchars method, and an empty string is returned if it does not exist

Go Summary of Thinkphp's curd easy to ignore

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.