first, CRUD
Thinkphp provides a flexible and convenient database operation method, The most basic is crud four operations, only master the most basic crud operation, can better use more practical database operation method
CRUD operations are usually coordinated with coherent operations
1. Create an action
Use the Add () method or the AddAll () method
Add () method
You can directly pass in the data you want to insert, and you will filter out fields that do not exist in the data table and illegal data types (such as objects, arrays, and Non-scalar data) before insertingthem. Returns the inserted record primary key value if the current table has a primary key autogrow and the insert record succeeds
If a data object (via the Create () method or the data () METHOD) has been created before the Add () operation, the value can not be passed in Add ()
AddAll () method
AddAll () method enables bulk data insertion
2. Read operation
In thinkphp, it is usually divided into read field (value, single Value), read record (one-dimensional array, individual record) and read record set (two-dimensional array, multiple Records)
• Read Field (value, single Value)
GetField () method
A single value is returned from a field that reads a record
Multiple fields that read a record return a one-dimensional associative array (the key is the first field value and the value is the remaining field Value) (the Rest is because the rest of the fields are not only one, and if the remaining fields are more than one, the values are separated by a space). ps, If the first field value is a numeric style (either int or char), an indexed array is formed (although in php, indexed arrays are not inherently different from associative arrays)!
• Read Records (one-dimensional arrays, single Records)
Find () method, which returns at most one record, even if there are multiple entries for the qualifying record, and only one is returned, so the limit () method is not valid for the Find () query
• Read Recordsets (two-dimensional arrays, multiple Records)
Select () method or FindAll () method
3. Update operation
Thinkphp mainly uses the Save () method to complete the update operation, as well as the SetField (), setinc (), setdec () methods
Save () Method
For the Save () method, the save () method does not perform an update operation if there is no where method () specifying an update condition or if the data object (or An array that is directly passed In) does not contain a primary key field . The goal is to ensure that the database is installed to avoid errors in updating all records in the table! Similar to the Add () method, if you have created a data object (via the Create () or data () METHOD) before calling the method, you can not pass the value
The return value is the number of rows affected and is of type int
SetField () method
If you only update a field, then also (why, because the Save () method can also update a field) you can use the SetField () method. Of course the SetField () method can also update multiple fields, passing array parameters
SetField (parameter 1, parameter 2); The parameter 1 represents the field name, and Parameter 2 indicates the value to be Updated. If parameter 1 is an array of element values for each field, then parameter 2 must also be an array of element values for each field relative to the value, and parameter 1 has several elements, parameter 2 must have a corresponding number of elements, here must correspond strictly, otherwise there will be a lot of unexpected situation
Setinc () method
Only for fields of numeric type , setinc (parameter 1, parameter 2, parameter 3); Parameter 1 indicates the field to be updated, parameter 2 represents the where condition, and parameter 3 indicates an increased value (3 ellipsis means 1 Increase)
Setdec () method
Only for fields of numeric type , Setdec (parameter 1, parameter 2, parameter 3); The parameter 1 represents the field to be updated, the parameter 2 represents the where condition, and parameter 3 represents a reduced value (3 omitted means 1 Less)
4. Delete operation
The delete () method, which is used to delete records, depends on the conditions defined in the where () method, and can also limit the number of records deleted by using the order () method and the Limit () method
second, Coherent operation
ThinkPHP2.0 full enablement of the coherent operation method can effectively improve the code clarity and development efficiency of data access. consistent operation for CRUD operations, not only with R operations
All coherent operations have only one parameter, and the parameters of the coherent operation are valid only in the current operation, and all the values of the coherent operation are automatically emptied after the operation is Completed. In short, the results of a coherent operation are not brought into future queries
For example, look at the first 3 records in the user table that meet the password 123456, and sort by ID
In a coherent operation, the order of the remaining methods is arbitrary except that the Select () method must be placed Last. Note that this is different from MySQL native SQL statement, the MySQL native SQL statement of the sub-statements are strictly sequential relationship , so the wording is equivalent to the above!
supplement, thinkphp not only supports coherent operation, but also supports querying directly using parameters, such as changing the code of the above coherent operation into the following form. Using array parameters, the key of the array is the method name of the coherent operation, and the value is the parameter of the coherent Operation.
1. where () method
The definition of a condition in a query, update, or delete operation, equivalent to a WHERE clause in a native SQL statement
Parameters support string, aarry, and object types
2. Table () method
Used to define the name of the data table to Manipulate. That is , you can use this method to define the data table that you want to manipulate if you want to manipulate a table that does not correspond to the model object by using a model object or to manipulate more than one table .
Note that the table name must be a full table name with a prefix, and you can use the alias
The parameter supports string and array types (the array type is used to alias the table, the key is the original, and the value is an Alias.) If you do not specify a value, you do not have an Alias) emphasize that the string type can also give the table an alias
3. Data () method
Used to pass data to model objects before creating, updating, and deleting operations
Note the difference from the Create () method, before discussing the difference between the two
4. Field () method
Definitions for the fields queried in the query operation
The parameter supports string and Aarry types (the array type is used to alias the field, the key is the original, and the value is an Alias.) If you do not specify a value, you cannot alias it). the string type can also be used to alias a field.
5. Order () Method
For sorting results
Supports multiple field sorting
Parameters support string and array types (array type, key is field name, value is Collation. If you do not specify a value, the default is Ascending)
6. Limit () Method
For result limits
In fact, the syntax for the limit clause is not the same for different databases, but thinkphp has shielded the difference for us at the bottom, so no matter what type of database it is, use the unified limit () method
Parameter supports string
7. Page () Method
Used for querying paging
Page (' page,[listrows] '); In a String-type argument, the page represents the current page, ListRows represents how many records per page, and if ListRows omitted, listrows the number of record bars in the Limit method
Note that this method is rarely used, but instead uses the paging class
8. Group () Method
For grouping
Parameter only supports string type
9. Having () method
For post-grouping filtering
Parameter only supports string type
10. Join () Method
For connection queries
Note that the join () method is the only method that can be called multiple times in a coherent operation
Parameters support string and array types
and the elimination of repeated fields has been implemented INTERNALLY.
11. Distinct () method
For the elimination of duplicate records, that is, unique filtering
Parameter supports BOOL type
12. Relation () Method
For associating operations
13. Lock () Method
Lock mechanism used in the database
Lock (true) automatically adds a for UPDATE after the generated SQL statement
ps, It is not difficult to see, these methods, if the parameter is a string type, then the parameter can be written in accordance with the native SQL corresponding clause syntax format! If the argument is of type array, be aware of the meaning of the key and the value in the different methods
thinkphp Tutorial _php Framework thinkphp (eight) "crud and coherent operations"