The ThinkPHP3.1 naming range feature provides a series of (coherent operation) encapsulation for model operations, making it easier to query and manipulate data. Let's take a concrete look at this usage below.
1. Defining attributes
To use the naming range feature, the _scope attribute definition of the model class and the use of the scope consistent operation method are mainly involved.
We first define the _scope attribute:
Class Newsmodel extends Model {
protected $_scope = Array (
//named range normal
' normal ' =>array (
' WHERE ' = >array (' status ' =>1),
),
//named range latest
' latest ' =>array (
' order ' => ' create_time DESC '),
' limit ' =>10,)
,
);
}
The _scope property is an array in which each array item represents the definition of a named range, and the defined format of the named range is:
' Named range ID ' =>array (
' property 1 ' => ' value 1 ',
' attribute 2 ' => ' value 2 ',
...
)
2. Named range identity : can be any string that identifies the currently defined named range.
The properties supported by the named range include:
where |
Query criteria |
Field |
Query fields |
Order |
Order of results |
Table |
Query table name |
Limit |
Result limit |
Page |
Result paging |
Having |
Having query |
Group |
Group Query |
Lock |
Query lock |
Distinct |
Unique Query |
Cache |
Query caching |
The definition of each named range can include one or more of these properties.
3. Method call
Once the property definition is complete, the next step is to invoke the named range using the scope method, and each invocation of a named range is equivalent to executing the associated action option defined in the named scope.
Call a named range
The simplest invocation method invokes a named range directly, for example:
$Model = D (' News '); The D method must be used here because the name range is defined inside the model
$Model->scope (' normal ')->select ();
$Model->scope (' latest ')->select ();
The resulting SQL statements are:
SELECT * from Think_news WHERE status=1
SELECT * to Think_news ORDER by Create_time DESC LIMIT 10
Calling multiple named scopes
You can also support calling multiple named range definitions at the same time, such as:
$Model->scope (' normal ')->scope (' latest ')->select ();
or simplified to:
$Model->scope (' normal,latest ')->select ();
The generated SQL is:
SELECT * from Think_news WHERE status=1 order by Create_time DESC LIMIT 10
If there is a conflict in the definition of two named ranges, the named range definition that is invoked later overrides the previous definition of the same attribute.
If the named range identity of the invocation does not exist, the named range is ignored, for example:
$Model->scope (' normal,new ')->select ();
New in the named range above does not exist, so only the normal named range takes effect, and the resulting SQL statement is:
SELECT * from Think_news WHERE Status=1
4. Default naming range
The system supports the default naming range feature, if you define a name range for the defaults, such as:
Protected $_scope = Array (
//default named range
' Default ' =>array (
' where ' =>array (' status ' =>1),
' Limit ' =>10,
),
);
Then the call to the default naming range can be used directly:
$Model->scope ()->select ();
Without having to pass in the name range ID again
$Model->scope (' Default ')->select ();
Although the two approaches are equivalent.
Name Range Adjustment
If you need to add additional adjustments based on the normal naming range, you can use:
$Model->scope (' normal ', array (' limit ' =>5))->select ();
The resulting SQL statement is:
SELECT * from Think_news WHERE Status=1 LIMIT 5
Of course, you can also make adjustments based on two named ranges, such as:
$Model->scope (' normal,latest ', Array (' limit ' =>5))->select ();
The generated SQL is:
SELECT * from Think_news WHERE status=1 order by Create_time DESC LIMIT 5
Custom Named range
Or, simply don't use any of the existing naming scopes, I'll just pass in a named range:
$Model->scope (Array (' field ' => ' id,title ', ' limit ' =>5, ' where ' => ' Status=1 ', ' Order ' => ' Create_time DESC '))->select ();
In this way, the resulting SQL becomes:
SELECT id,title from Think_news WHERE status=1 order by Create_time DESC LIMIT 5
5. Mixed use with coherent operations
Named ranges can be mixed with previous coherent operations, such as defining a named range _scope attribute:
Protected $_scope = Array ('
normal ' =>array ('
where ' =>array (' status ' =>1),
' field ' => ' ID, Title ',
' limit ' =>10,
),
);
Then, when used, you can call this:
$Model->scope (' normal ')->limit (8)->order (' id desc ')->select ();
In this way, the resulting SQL becomes:
SELECT id,title from Think_news WHERE status=1 ORDER BY id DESC LIMIT 8
If there is a conflict between the defined naming range and the properties of the consecutive operation, the one that is called overwrites the preceding.
If this is called:
$Model->limit (8)->scope (' normal ')->order (' id desc ')->select ();
The resulting SQL is:
SELECT id,title from Think_news WHERE status=1 ORDER BY id DESC LIMIT 10
Summarize
The advantage of the naming range feature is that you can define multiple calls at once , and you can also work together in a project to avoid problems when developers write curd operations, and project managers only need a reasonable range of planning names.