The Think\model class in thinkphp provides a basic curd of the database (Create, Update, read, and delete), which can be easily manipulated.
The main methods of model classes and extension classes are:
- Create Operation:
Create () (non-chained), add (), AddAll ()
The supported chain operations are:
Table, data, field, relation, validate, auto, filter, scope, bind, token, comment
- Update operation
Save (), SetField (), Setinc (), Setdec ()
The supported chain operations are:
Where, table, alias, field, order, lock, Relation, scope, bind, comment
- Read operation
Find (), select (), GetField (), Count, Max, Min, AVG, Sum
The supported chain operations are:
Where, table, alias, field, order, group, having, join, union, DISTINCT, lock, cache, relation, result, scope, bind, comment
- Delete operation
Delete ()
The supported chain operations are:
Where, table, alias, order, lock, Relation, scope, bind, comment
The possible return values of these methods are false/true/an instance of an array/two-dimensional array/numeric (number)/null/class itself, which is now summarized as follows:
1, chain-operated:
So the chained operation returns an instance of the model class, that is, the return value can be used to continue invoking the instance method.
$User=M("User");
$U1=$User->$where("id = 1");//$U1可以继续调用其他方法。
$result=$U1->select();
//-------一般情况下没有必要这样,下面一句代码即可实现
$result=M("User")->$where("id = 1")->select();
2. Create Operation
- Create return value:
- returns False if an error occurs,
- success returns the created data
Create is a non-chained operation and the return value is a possible boolean value, so the operation is strictly judged:
$User = M" User "
if ( $User ->create ()) {
//do more
-
}
- Add return value, AddAll return value
- success: If the primary key is an autogrow type, the return value is the new record ID value (AddAll is the maximum value), otherwise it returns true;
- error has returned false;
3. Update Operation
- Save return value (SetField, Setinc, Setdec the last call is the Save method, so the return value is consistent with Save):
The Save return value is the number of records affected (possibly 0), the update error returns false, so be sure to use identity to determine if the update failed.
4. Read Operation
- Find return value
If the query fails, the return value of find is False
If the query result is null and returns NULL, the query returns an associative array (the key value is the field name or alias).
- Select return value
If the query is faulted, the return value of select is False,
Returns NULL if the query result is empty, otherwise a two-dimensional array is returned.
- GetField return value
- If the query is faulted, the return value of GetField is False
- Query success:
- GetField ($field): $field is a field that returns the value of the first row of the field in the database
- GetField ($field, True): $field A field that returns a one-dimensional array containing the field
- GetField ($fields): The $fields is two fields (separated by ","), returning an array of arrays ($field 1=> $field 2), and if $field is multiple fields, returns a multidimensional array similar to select, with the key named Field1.
- GetField ($fields, ":"): $field as multiple fields, returning an array similar to the one in array ($field 1=> "$field 2: $field 3")
- Count, Max, Min, AVG, sum return value
If the query is faulted, the return value is False
If the query succeeds, the corresponding value is returned
5. Delete Operation
Delete return Value: Success returns TRUE, query error returns false
Source: http://www.baiwar.com/post/thinkphp-database-operations-in-the-return-value.html
Go "thinkphp database operation return value summary