Tips and tricks for using thinkphp (i)

Source: Internet
Author: User

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

Such as:
Data table Name: prefix _ table name
Model Class Name: Table name Model NOTE: The first letter of the table name here is capitalized
Create object: D (' table name ') Note: The first letter of the table name here is capitalized

defining the Controller class
Class Indexaction extends action{
Public function Show () {
Echo ' This is the new show operation ';
}
}
Then enter it in the browser
http://localhost/myApp/index.php/Index/show/


To define a model class:

Class table name Model extends model{
[//manually define fields [optional]
Protected $fields = Array (
' ID ',
' Username ',
' Email ',
' Age ',
' _PK ' = ' id ',//primary key
' _autoinc ' =>true//whether self-increment
)
]
}

Changes to the record:

$User = D ("user")//instantiation of User Object
$User->find (1)//Find records with ID 1
$User->name = ' thinkphp '//Change the Name field of the found record to thinkphp
$User->save ()//Save modified data
Update values for a specific field
$User->setfield (' name ', ' Topthink ', ' id=1 ')
You can also support operations on fields
$User->setfield (' Score ', ' (score+1) ', ' id=1 ')


new Record, Method 1:
$User = new Usermodel ()//Instantiate User Object
$User, field name = field value//Assign value to field
$User->add ()//Add record
new Record, Method 2:
$data [' field name '] = field value; Assigning a value to a field
$User = D (' User '); Instantiating a User object
$User->add ($data); $insertId, the return value of the Add method is the most recently inserted primary key value, which can be obtained directly.
added multiple records:
$User = new Usermodel ()
$data [0][' name '] = ' thinkphp '
$data [0][' email '] = ' [email protected] '
$data [1][' name '] = ' fleeting '
$data [1][' email '] = ' [email protected] '
$User >addall ($data)


Deleting Records

$User->find (2)
$User->delete ()//delete a found record
$User->delete (' 5,6 ')//delete data with primary key 5, 6
$User->deleteall ()//delete all data from the query

Record Query

$User->getdbfields ()//Get the current data field
$User->findall (); Find All records
$User->findall (' 1,3,8 ')//querying the Recordset with primary key 1,3,8
$User->count ()//Get the number of records
$User->max (' score ')//Get the user's maximum points
$User->min (' score ', ' score>0 ')//earn minimum points for users with points greater than 0
$User->avg (' field name ')//Get the average of field values for all records
$User->sum (' field name ')//Statistic field value
(The use of the following methods must inherit the Advanced model Class)
$User->getn (2,array (' score>80 '))//Return to qualifying 2nd record
$User->getn ( -2,array (' score>80 '))//can also get the last second record
$User->first (Array (' score>80 ', ' score desc '))//If you want to query the first record, you can also use the
$User->last (Array (' score>80 ', ' score desc '))//Get the last record
$User->top (5,array (' score desc) ')//Get the top 5 points
Querying Records $User->getby (' name ', ' liu21st ')//followed by field values

$Model = new Model ()//Instantiate a model object without any data table
$Model->query ("select * from Think_user where Status=1")//direct use of native SQL statements

$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 for updating and writing data, returning the number of records affected


$User->starttrans ()//Start transaction
$User->commit ()//Submit Transaction
$User->rollback ()//transaction rollback

Template:

$this->assign (' name ', $value); Use the Assign method in the Action class to assign a value to the template variable, regardless of which variable type is uniformly used assign assignment

$this->display ()//Output template file

batch assignment, assign single-parameter use
$array [' name '] = ' thinkphp '
$array [' email '] = ' [email protected] '
$array [' phone '] = ' 12335678 '
$this->assign ($array)


$this->display ()//Call the User module's Read action template
$this->display (' edit ')//Call the User module's edit action template
$this->display (' member:read ')//Call the Read action template of the Member module
$this->display (' [email protected]:edit ')//Edit action template of User module calling Xp theme
$this->display ('.. /member/read.html ')//Specify the full name of the template file directly

Template Tags:

{} or {//comment content}//Template comment
{$user [' name ']}//output array variable
{$user: Name}//properties of the Output object

To facilitate template definition, the output template variable can be output in the following uniform way, regardless of whether it is an array or an object:
{$user. Name}
If the output of a multidimensional array or multi-layered object property, use the following definition:
{$user [' Sub '] [' name ']}
{$user: Sub:name}

to use a function in a template:
Format: {$varname |function1|function2=arg1,arg2,###} or {: function (parameter 1, parameter 2)}
Description
There can be no spaces between {and $ symbols, there is no problem with the space behind the argument
# # #表示模板变量本身的参数位置

System Variables
{$Think. Server.script_name}//Get $_server variable
{$Think. SESSION.SESSION_ID|MD5}//Get $_session variable
{$Think. Get.pagenumber}//Get $_get variable
{$Think. Cookie.name}//Get $_cookie variable
System Constants
{$Think. const.__file__}
{$Think. Const.module_name}
special variables, constants defined by the thinkphp system
{$Think. Version}//Versions
{$Think. Now}//Current time

Quick Output (3.0 and later version removed)
{: function (...)} Executes the method and outputs the return value
{~function}//execution method does not output
{@var}//Output Session variable
{&var}//Output configuration parameters
{%var}//Output language variable
{. var}//Output GET variable
{^var}//Output POST variable
{*var}//Output constant

include external files
<include file= "$tplName"/>//Use variables to control the template to be imported
<include file= "/apps/home/tpl/simple/public/header.html"/>//Use a full file name containing
Note: You cannot mix variables with strings, such as: <include file= "user/$tplName"/>//This is wrong

Tips and tricks for using thinkphp (i)

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.