Bugs and skills in thinkphp

Source: Internet
Author: User

Thinkphp bug and tips:
1. tags not available in the template
{$ Content} {$ I}
2. If tag
For example: <if condition = "$ name EQ 1">
There are always unexpected errors after the test, so it is better to use them directly <? PHP if (...) {...?> Faster.

Conventions:
1. All class library files must use. Class. php as the file suffix, and the class name and file name must be consistent.
2. The class name of the controller is suffixed with action.
3. The model class name is suffixed with model. The first letter of the class name must be capitalized.
4. All database table names are in lower case,

For example:
Data Table Name: prefix _ table name
Model Class Name: Table Name model note: the first letter of the table name must be capitalized.
Create object: D ('table name') Note: The first letter of the table name must be capitalized.

Define controller class
Class indexaction extends action {
Public Function show (){
Echo 'this is the new show operation ';
}
}
Enter
Http: // localhost/MyApp/index. php/index/show/

Define model class:
Class table name model extends model {
[// Manually define a field [Optional]
Protected $ fields = array (
'Id ',
'Username ',
'Email ',
'Age ',
'_ PK' => 'id', // primary key
'_ Autoinc' => true // whether to auto-Increment
)
]
}

Record modification:
$ User = D ("user") // instantiate the user object
$ User-> Find (1) // search for records with ID 1
$ User-> name = 'thinkphp' // modify the name field of the queried record to thinkphp.
$ User-> Save () // Save the modified data
Update the value of a specific field
$ User-> setfield ('name', 'topthink', 'Id = 1 ')
Field operations are also supported.
$ User-> setfield ('score ',' (score + 1) ', 'Id = 1 ')

Create record, Method 1:
$ User = new usermodel () // instantiate the user object
$ User-> Field name = field value // assign a value to the field
$ User-> Add () // Add record
Create record, Method 2:
$ Data ['field name'] = field value; // assign a value to the field
$ User = D ('user'); // instantiate the user object
$ User-> Add ($ data); // $ insertid. the return value of the add method is the newly inserted primary key value, which can be obtained directly.
Add multiple records:
$ User = new usermodel ()
$ Data [0] ['name'] = 'thinkphp'
$ Data [0] ['email '] = 'thinkphp @ gmail.com'
$ Data [1] ['name'] = 'current year'
$ Data [1] ['email '] = 'liu21st @ gmail.com'
$ User> addall ($ data)

Delete record
$ User-> Find (2)
$ User-> Delete () // Delete the found record
$ User-> Delete ('5, 6') // delete data with a primary key of 5 and 6
$ User-> deleteall () // Delete All queried data

Record Query

$ User-> getdbfields () // obtain the current data field
$ User-> findall (); // query all records
$ User-> findall ('1, 3, 8 ') // query Record Sets whose primary key is 1, 3, and 8.
$ User-> count () // obtain the number of records
$ User-> MAX ('score ') // obtain the maximum credits of a user
$ User-> min ('score ', 'score> 0') // obtain the minimum credits of a user whose points are greater than 0.
$ User-> AVG ('field name') // obtain the average value of the field values of all records
$ User-> sum ('field name') // calculates the field value.
$ User-> getn (2, 'score> 80', 'score desc') // returns the 2nd matching records.
$ User-> getn (2, 'score> 80', 'score desc') // you can obtain the last second record.
$ User-> first ('score> 80', 'score desc') // to query the first record, you can also use
$ User-> last ('score> 80', 'score desc') // obtain the last record.
$ User-> top (5, '', 'score desc') // obtain the first five records with the highest points
$ User-> getby ('name', 'liu21st') // query the field value of the data field

$ Model = new model () // instantiate a model object without any data tables
$ Model-> query ("select * From think_user where status = 1 ")

$ Objrs = $ model-> query ("select * From think_user where status = 1") // custom Query
$ Model-> execute ("Update think_user set name = 'thinkphp' where status = 1") // SQL operation used to update and write data, returns the number of affected records

$ User-> starttrans () // start the transaction
$ User-> commit () // submit the transaction
$ User-> rollback () // transaction rollback

Template:

$ This-> assign ('name', $ value); // use the Assign Method in the action class to assign values to template variables. All variable types Use assign for assignment.

$ This-> display () // output Template File

Batch assignment
$ Array ['name'] = 'thinkphp'
$ Array ['email '] = 'liu21st @ gmail.com'
$ Array ['phone'] = '000000'
$ This-> assign ($ array)

$ This-> display () // call the read operation template of the User Module
$ This-> display ('edit') // call the edit operation template of the user module.
$ This-> display ('Member: read') // call the read operation template of the member module.
$ This-> display ('xp @ User: edit') // call the edit operation template of the user module of the XP topic.
$ This-> display ('../member/read.html') // specify the full name of the template file.

Template Tag:

{/* Comment content */} or {// comment content} // template comment
{$ User ['name']} // output array variable
{$ User: name} // attributes of the output object

To facilitate template definition, you can use the following methods to output template variables in an array or an object:
{$ User. name}
For multi-dimensional array or multi-layer object attribute output, use the following definition method:
{$ User ['sub'] ['name']}
{$ User: Sub: name}

Function:
Format: {$ varname | function1 | function2 = arg1, arg2 ,###}
Note:
{There cannot be spaces between the symbol and the $ symbol, so there is no problem with the space of the following parameter.
### Parameter location of template Variables

System Variables
{$ Think. server. script_name} // get the $ _ server variable.
{$ Think. session. session_id | MD5} // get the $ _ session variable
{$ Think. Get. pagenumber} // get the $ _ Get variable.
{$ Think. Cookie. name} // get the $ _ cookie variable
System constant
{$ Think. Const. _ file __}
{$ Think. Const. module_name}
Special variables, constants defined by the thinkphp System
{$ Think. Version} // version
{$ Think. Now} // current time

Quick output
{: Function (...)} // Execute the method and output the returned value
{~ Function} // The execution method is not output
{@ Var} // output the session variable
{& Var} // output configuration parameters
{% Var} // output language variable
{. Var} // output get variable
{^ Var} // output the post variable
{* Var} // output constant

Contains external files
<Include file = "$ tplname"/> // use variables to control the template to be imported
<Include file = "../public/header.html"/> // use a complete file name to include

Loop output
Iterate also has other aliases, including volist, resultset, and sublist.

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 5th ~ of list ~ 15 records
<Iterate name = "list" id = "VO" offset = "5" length = '10'>
{$ VO. name}
</Iterate>

// Output an even number of records
<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>

// Subloop output
<Volist name = "list" id = "VO">
<Sublist name = "VO ['sub']" id = "sub">
{$ Sub. name}
</Sublist>
</Volist>

Switch label
<Switch name = "name">
<Case value = "1"> value1 </case>
<Case value = "2"> value2 </case>
<Default/> default
</Switch>
The name attribute can use functions and system variables, for example:
<Switch name = "think. Get. userid | abs">
<Case value = "1"> admin </case>
<Default/> default
</Switch>
You can also use variables for the value attribute of case, for example:
<Switch name = "userid">
<Case value = "$ adminid"> admin </case>
<Case value = "$ memberid"> member </case>
<Default/> default
</Switch>

Compare tags
<EQ name = "name" value = "value"> value </EQ> // if the value of the name variable is equal to the value, the output is
<NEQ name = "name" value = "value"> value </NEQ> // if the value of the name variable is not equal to the value, the output is
<GT name = "name" value = "5"> value </GT> // output if the value of the name variable is greater than 5.
<Egt name = "name" value = "5"> value </egt> // output if the value of the name variable is greater than or equal to 5.
<Lt name = "name" value = "5"> value </LT> // output if the value of the name variable is smaller than 5.
<ELT name = "name" value = "5"> value </ELT> // if the value of the name variable is less than or equal to 5, the output is

// In fact, all the above labels are the aliases of compare labels.
// The value of the type attribute is the name of the judgment tag listed above.
<Compare name = "name" value = "5" type = "EQ"> value </compare> // if the value of the name variable is 5, the output is

If tag
<If condition = "$ name EQ 1"> value1
<Elseif condition = "$ name EQ 2"/> value2
<Else/> value3
</If>

C operation
Operation (dynamic) configuration: Mainly used in the Action Method
Obtain:
C ('configuration parameters ')
Settings:
C ('configuration parameter', new value)

Operation
Quickly create an action object:
$ Action = a ('user ');
Equivalent
$ Action = new useraction ();

D operation
Quickly create model data objects:
$ Model = D ('user ');
Equivalent
$ Model = new usermodel ();

S operation
Quick cache operations
Obtain:
S ('name ')
Settings:
S ('name', 'value ');
Delete:
S ('name', null );

F Operation
Saved file data
Use the same method as s

L operation
Quick operation of language Variables
Obtain:
L ('language variable ');
Settings:
L ('language variable', 'value ');
For example, L ('user _ info', 'user information'); // set the language variable named user_info
Batch assignment:
$ Arr ['language variable 1'] = 'value 1 ';
$ Arr ['language variable 2'] = 'value 2 ';
L ($ ARR );

Create procedure procedure1/* Name stored procedure name */

(In parameter1 integer)/* parameters parameter */

Begin/* Start of block statement header */

Declare variable1 char (10);/* variables variable Declaration */

If parameter1 = 17 then/* Start of if condition start */

Set variable1 = 'birds ';/* assignment value */

Else

Set variable1 = 'beasts';/* assignment value */

End if;/* end of if end */

Insert into Table1 values (variable1);/* Statement SQL statement */

End/* end of block statement block end */

Thinkphp system constant

Think_path // thinkphp system directory
App_path // current project directory
App_name // current project name
Module_name // name of the current Module
Action_name // current operation name
Tmpl_path // Project template directory
Lib_path // directory of the Project class library
Cache_path // Project template cache directory

Config_path // project configuration file directory
Log_path // project log file directory
Lang_path // project language file directory
Temp_path // temporary project file directory
Plugin_path // directory of the Project plug-in file
Vendor_path // third-party class library directory
Data_path // project data file directory
Is_apache // whether it belongs to Apache
Is_iis // whether it belongs to IIS
Is_win // whether it belongs to the Windows Environment
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 the memory usage limit exists

Memory_limit_on // whether the memory usage limit exists
Output_gzip_on // whether to enable output Compression
Magic_quotes_gpc // magic_quotes_gpc
Think_version // thinkphp version
Lang_set // browser Language
Template_name // name of the current template
Template_path // current template path
_ Root _ // website root directory address
_ App _ // current project (entry file) Address
_ URL _ // address of the current Module
_ Action _ // current operation address
_ Self _ // current URL
Tmpl_file_name // default Template Name (including path) for the current operation)
Web_public_url // public website directory
App_public_url // Project Public 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 // log recorded in the system mode
Mail_log = 1 // mail method log
Tcp_log = 2 // log recorded in TCP Mode
File_log = 3 // log recorded by file
Data_type_obj = 1 // returns an object
Data_type_array = 0 // returns an array
Url_common = 0 // URL in Normal Mode
Url_pathinfo = 1 // pathinfo URL
Url_rewrite = 2 // rewrite URL
Has_one = 1 // has_one Association Definition
Belongs_to = 2 // belongs_to Association Definition
Has_attributes = 3 // has_attributes Association Definition
Many_to_detail = 4 // many_to_detail Association Definition
Exists_to_vailidate = 0 // validate if a field exists in the form
Must_to_validate = 1 // verification required
Value_to_vailidate = 2 // verify if the form value is not empty

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.