The so-called CURD. That is, four basic operations (CURD) for database operations: C: create (create), U: update (update), R: read (read), and D: detele (delete ).
In ThinkPHP, these methods are not necessarily used. Here we list common methods such as select, find, findAll, save, and create:
D read:
Select-> () query the dataset, which is the same as findAll->. For example:
$ User-> where ('status = 1')-> order ('create _ Time')-> limit (10)-> select ();
Note: In a coherent operation, except that the select method must be placed in the last one, the call sequence of other coherent operations is not sequential. For example, the following code is equivalent to the preceding one:
$ User-> order ('create _ Time')-> where ('status = 1')-> limit (10)-> select ();
If you are not used to consistent operations, the new feature version supports directly using the parameter. For example, the ghost code above can be modified.
Written:
$ User-> select (array ('order' => 'create _ time', 'where' => 'status = 1 ', 'limit' => '10 '));
The find-> () method is similar to the above two methods. The difference is that only one data record is returned. It can be used with getField-> () to obtain a field value of a record.
Select is the same as findall. A two-dimensional array is returned. For example
Array (1 ){
[0] => array (8)
{["Rank_id"] => string (3) "151 ″
["Rank_name"] => string (7) "test 9 ″
["Rank_memo"] => string (3) "123 ″
["Uid"] => string (5) "59471 ″
["Rank_kw"] => string (6) "important"
["Rank_uptime"] => string (10) "1280202914 ″
["Isverify"] => string (1) "0 ″
["Ishot"] => string (1) "0 ″
}
}
Find returns a one-dimensional array:
Array (8 ){
["Rank_id"] => string (3) "151 ″
["Rank_name"] => string (7) "test 9 ″
["Rank_memo"] => string (3) "123 ″
["Uid"] => string (5) "59471 ″
["Rank_kw"] => string (6) "important"
["Rank_uptime"] => string (10)"
1280202914 "[" isverify "] => string (1)" 0 ″
["Ishot"] => string (1) "0 ″
}
Where Method: Used to query or update conditions.
Table Method: defines the name of the data Table to be operated.
$ Model-> Table ('Think _ user user')-> where ('status> 1')-> select ();
Field Method: Define the field to be queried
Field PARAMETERS support strings and arrays, for example,
$ Model-> field ('Id, nickname as name')-> select ();
$ Model-> field (array ('id', 'nickname' => 'name')-> select ();
If fields are not specified using the field method, field ('*') is equivalent by default.
U update, C creation:
Data, add, and save Methods: assign values to data objects, add, and save data objects. For example:
$ Data ['name'] = 'thinkphp ';
$ Data ['email '] = 'thinkphp @ gmail.com ';
$ Model-> data ($ data)-> add (); // add, equivalent to insert, consistent writing
$ Model-> add ($ data); // add, equivalent to insert, non-coherent writing
$ Model-> data ($ data)-> where ('Id = 3')-> save (); // modify, equivalent to update
Note that, if the data does not change in the save method, the Operation returned by default is FALSE. However, this save operation is OK.
Create-> () automatically forms the data in the form of $ data from the POST Field
$ User = D ("User ");
$ User-> create (); // by default, the data submitted through the form is created.
$ User-> add (); // add
If the primary key of your primary key is auto-increment, if the primary key is successfully merged into the data, the value returned by the Add method primary key is the value of the latest primary key of the primary key, and you can directly obtain the primary key.
If you use the data method to create a volume data object, the volume will be automatically verified and filtered. But add the quit in the quit line.
When the "save" operation is performed, the data table contains the "deny" field and the invalid "deny" data type (such as non-standard data such as objects and arrays ).
Filters out partitions and worries about the SQL error caused by writable Writing of non-data table fields.
We are familiar with the token verification, self-Token verification, and self-token (we will see the token-related token usage later) functions.
The create method is required to take effect. The data object created by the Create method is stored in the memory, and the producer is not actually written to the producer database.
. To create a data object, you need to complete some additional features.
In other words, you can use the data method to create a data object in simple sequence.
SetInc and setDec methods. Update a statistical field (usually a number:
$ Model-> setInc ('score ', 'Id = 5', 3); // Add 3 points to a user
$ Model-> setInc ('score ', 'Id = 5'); // Add 1 to your credit
$ Model-> setDec ('score ', 'Id = 5', 5); // reduce the user's points by 5
$ Model-> setDec ('score ', 'Id = 5'); // reduce the user's points by 1
D. Delete:
Delete-> () delete data
$ User-> where ('status = 0')-> order ('create _ Time')-> limit ('5')-> delete ();
Other common methods of Model:
Order Method: The result sorting is as follows:
Order ('Id desc ')
The sorting method supports sorting multiple fields.
Order ('status desc, id asc ')
The parameters of the order Method support strings and arrays. The array usage is as follows:
Order (array ('status' => 'desc', 'id '))
Limit Method: Result Restriction
Limit ('1, 10 ′)
If limit ('10') is used, it is equivalent to limit ('0, 10 ′)
The page method is used to query pages. The Page method is similar to the limit method in the following format:
Page ('page [, listRows] ')
Page indicates the current Page number, and listRows indicates the number of records displayed on each Page. For example, if 10 records are displayed on each page, the data on the 2nd page is obtained:
Page ('2, 10 ′)
If listRow is not written, the limit ('length') value is read. For example, if 25 records are displayed on each page, the following data is obtained:
Limit (25)-> page (3 );
If limit is not set, 20 records are displayed on each page by default.
Join method: Query Join supports. Join parameters support strings and arrays, And the join method is the only method that can be called multiple times in a coherent operation. For example:
$ Model-> join ('work ON artist. id = work. artist_id ')-> join ('Card ON artist. card_id = card. id')-> select ();
The left join method is used by default. If you want to use another JOIN method, you can change it
$ Model-> join ('Right JOIN work ON artist. id = work. artist_id ')-> select ();
Distinct method: supported Disiinct. Unique filtering during Data Query
$ Model-> Distinct (true)-> select ();
Relation method: supports association query.
$ Model-> Relation (true)-> select ();
Conditional Query
$ Map-> put ('name', 'php'); // name = 'php'
('Name', array ('like', 'think'); // name like '...'
('Id', array ('in', array (1, 2, 4 )));
('Id', array ('10', '3', 'or'); // id> = 10 or <= 3
Thinkphp multi-Table query statement
1. table () function
A table () function is provided in thinkphp. For specific usage, refer to the following statements:
$ List = $ Demo-> table ('Think _ blog, think_type type')-> where ('blog. typeid = type. id')-> field ('blog. id as id, blog. title, blog. content, type. typename as type ')-> order ('blog. id desc ')-> limit (5)-> select ();
Echo $ Demo-> getLastSql (); // print the SQL statement and check it
2. join () function
Take a look at the Code:
$ Demo = M ('artlist ');
$ Demo-> join ('Right JOIN think_work ON think_artist.id = think_work.artist_id ');
// You can use inner join or left join. Note the table name prefix!
Echo $ Demo-> getLastSql (); // print the SQL statement and check it
The simplest code for adding, deleting, querying, and modifying
[Php]
// Query data
Public function index (){
$ Form = M ("Form ");
$ List = $ Form-> limit (5)-> order ('Id desc')-> findAll ();
$ This-> assign ('LIST', $ list );
$ This-> display ();
}
// Write data
Public function insert (){
$ Form = D ("Form ");
If ($ vo = $ Form-> create ()){
$ List = $ Form-> add ();
If ($ list! = False ){
$ This-> success ('data is saved successfully! ');
} Else {
$ This-> error ('data writing error! ');
}
} Else {
$ This-> error ($ Form-> getError ());
}
}
// Update data
Public function update (){
$ Form = D ("Form ");
If ($ vo = $ Form-> create ()){
$ List = $ Form-> save ();
If ($ list! = False ){
$ This-> success ('data update successful! ');
} Else {
$ This-> error ("No data is updated! ");
}
} Else {
$ This-> error ($ Form-> getError ());
}
}
// Delete data
Public function delete (){
If (! Emptyempty ($ _ POST ['id']) {
$ Form = M ("Form ");
$ Result = $ Form-> delete ($ _ POST ['id']);
If (false! ==$ Result ){
$ This-> ajaxReturn ($ _ POST ['id'], 'deleted successfully! ', 1 );
} Else {
$ This-> error ('deletion error! ');
}
} Else {
$ This-> error ('the deleted item does not exist! ');
}
}