The most comprehensive THinkPHP experience and skills in history are better than the manual

Source: Internet
Author: User
The most comprehensive THinkPHP experience and skills in history are better than the manual
THinkPHP is the most comprehensive experience and skill in history. it is better to use THinkPHP than a manual. never miss THinkPHP.
THinkPHP tips and techniques (a) http://www.jb100.net/html/content-28-475-1.html
THinkPHP tips and techniques (II) http://www.jb100.net/html/content-28-477-1.html
THinkPHP tips and techniques (3) http://www.jb100.net/html/content-28-479-1.html
THinkPHP tips and techniques summary (4) http://www.jb100.net/html/content-28-248-1.html

This is the most comprehensive THINKPHP usage summary in history. it is better to use THINKPHP than the manual and has detailed instructions on various usage. it is especially suitable for beginners. never miss it when you pass!

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 ')
The same example supports field operations.
$ 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 '] = 'sjolzy @ chen.com'
$ Data [1] ['name'] = 'current year'
$ Data [1] ['email '] = 'Chen @ sjolzy.cn'
$ 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 '] = 'Chen @ sjolzy.cn'
$ 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:

{} 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
.............

Because there are too many texts, here are only some of my experiences and tips. more highlights are as follows:

THinkPHP tips and techniques (a) http://www.jb100.net/html/content-28-475-1.html
THinkPHP tips and techniques (II) http://www.jb100.net/html/content-28-477-1.html
THinkPHP tips and techniques (3) http://www.jb100.net/html/content-28-479-1.html
THinkPHP tips and techniques summary (4) http://www.jb100.net/html/content-28-248-1.html

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.