The naming range Function of ThinkPHP3.1 provides a series of (consistent operations) Encapsulation for model operations, allowing you to conveniently query and operate data. Next, let's take a look at this usage.
1. Define attributes
To use the naming range function, we mainly need to define the _ scope attribute of the model class and use the coherent operations of the scope.
First, we define the _ scope attribute:
Class NewsModel extends Model {protected $ _ scope = array (// name range: normal 'normal' => array ('where' => array ('status' => 1 ),), // name range: latest 'latest' => array ('order' => 'create _ time DESC ', 'limit' => 10 ,),);}
The _ scope attribute is an array. Each array entry defines a naming range. The format of the naming range is as follows:
'Name range identifi' => array ('Property 1' => 'value 1', 'Property 2' => 'value 2 ',...)
2. Name range identifier: Can be any string used to identify the name range of the current definition.
The following attributes are supported:
Where |
Query Conditions |
Field |
Query Fields |
Order |
Result sorting |
Table |
Query table name |
Limit |
Result restrictions |
Page |
Result pagination |
Having |
Having Query |
Group |
Group Query |
Lock |
Query lock |
Distinct |
Unique Query |
Cache |
Query Cache |
The definition of each naming range can include one or more of these attributes.
3. method call
After the attribute is defined, the scope method is used to call the naming range. Each call to a naming range is equivalent to executing the related operation options defined in the naming range.
Call a naming range
The simplest call method is to directly call a specified name range, for example:
$ Model = D ('new'); // The D method must be used here because the name range defines $ Model-> scope ('normal')-> select () in the Model (); $ Model-> scope ('latest ')-> select ();
The generated SQL statements are:
SELECT * FROM think_news WHERE status=1SELECT * FROM think_news ORDER BY create_time DESC LIMIT 10
Call multiple naming ranges
You can also call multiple naming ranges at the same time, for example:
$Model->scope('normal')->scope('latest')->select();
Or simplified:
$Model->scope('normal,latest')->select();
The generated SQL statements are:
SELECT * FROM think_news WHERE status=1 ORDER BY create_time DESC LIMIT 10
If the definitions of the two naming ranges conflict, the naming range definitions called later will overwrite the previous definitions of the same attribute.
If the called name range identifier does not exist, the name range is ignored. For example:
$Model->scope('normal,new')->select();
The new clause in the above naming range does not exist, so only the normal naming range takes effect. The generated SQL statement is:
SELECT * FROM think_news WHERE status=1
4. default naming range
The system supports the default naming range function. If you define a default naming range, for example:
Protected $ _ scope = array (// default name range: 'default' => array ('where' => array ('status' => 1 ), 'limit' => 10 ,),);
You can directly call the default naming range:
$Model->scope()->select();
Instead of passing in the name range identifier
$Model->scope('default')->select();
Although these two methods are equivalent.
Adjust the name range
If you need to add additional adjustments based on the normal naming range, you can use:
$Model->scope('normal',array('limit'=>5))->select();
The generated SQL statement is:
SELECT * FROM think_news WHERE status=1 LIMIT 5
Of course, you can also adjust the two naming ranges, for example:
$Model->scope('normal,latest',array('limit'=>5))->select();
The generated SQL statement is:
SELECT * FROM think_news WHERE status=1 ORDER BY create_time DESC LIMIT 5
Custom name range
Alternatively, you do not need to use any existing naming ranges. I directly input a naming range:
$Model->scope(array('field'=>'id,title','limit'=>5,'where'=>'status=1','order'=>'create_time DESC'))->select();
In this way, the generated SQL becomes:
SELECT id,title FROM think_news WHERE status=1 ORDER BY create_time DESC LIMIT 5
5. Mixed use with coherent operations
The same name range can be used in combination with the previous coherent operation. For example, the name_scope attribute is defined:
protected $_scope = array( 'normal'=>array( 'where'=>array('status'=>1), 'field'=>'id,title', 'limit'=>10, ), );
Then, you can call the following method when using it:
$Model->scope('normal')->limit(8)->order('id desc')->select();
In this way, the generated SQL becomes:
SELECT id,title FROM think_news WHERE status=1 ORDER BY id desc LIMIT 8
If the defined naming range conflicts with the attribute of a coherent operation, the subsequent call overwrites the previous one.
If so, call:
$Model->limit(8)->scope('normal')->order('id desc')->select();
The generated SQL statement is:
SELECT id,title FROM think_news WHERE status=1 ORDER BY id desc LIMIT 10
Summary
The advantage of the naming range function is that you can define multiple calls at a time.In addition, the project can also adopt a division of labor to avoid problems when developers write CURD operations. The Project Manager only needs to properly plan the naming scope.