The naming range function provides a series of (consistent operation) encapsulation for model operations, allowing you to conveniently query and operate data. Let's take a look at this usage. The naming range function provides a series of (consistent operation) encapsulation for model operations, allowing you to conveniently query and operate data. Let's take a look at this usage.
To define attributes, you must use the naming range function, which mainly involves
_ Scope attributesDefinition and
Scope coherent operationsMethod.
First, we define the _ scope attribute:
- Class NewsModel extends Model {
- Protected $ _ scope = array (
- // The name range is normal.
- 'Normal' => array (
- 'Where' => array ('status' => 1 ),
- ),
- // Name range: latest
- 'Latest '=> array (
- 'Order' => 'Create _ time DESC ',
- 'Limit' => 10,
- ),
- );
- }
The copy code _ scope attribute is an array. each array item 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 ',
- ...
- )
Copy code
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.
After the method call property definition is complete, use
Scope methodWhen a name range is called, each call to a name range is equivalent to executing the related operation options defined in the name range. The simplest way to call a naming range is to directly call a naming range. for example:
- $ Model = D ('New'); // The D method must be used here because the name range is defined in the Model.
- $ Model-> scope ('normal')-> select ();
- $ Model-> scope ('latest ')-> select ();
The SQL statements generated by the copied code are:
- SELECT * FROM think_news WHERE status = 1
- SELECT * FROM think_news order by create_time desc limit 10
You can also call multiple naming ranges at the same time by copying code to call multiple naming ranges. for example:
- $ Model-> scope ('normal')-> scope ('latest ')-> select ();
Copy the code or simplify it:
- $ Model-> scope ('normal, latest ')-> select ();
The SQL statements generated by the copied code are:
- SELECT * FROM think_news WHERE status = 1 order by create_time desc limit 10
Copy the code. if the definitions of the two naming ranges conflict, the name range definition called later will overwrite the previous definition 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 statement does not exist in the naming range above the Copy code. Therefore, only the normal naming range takes effect. the generated SQL statement is:
- SELECT * FROM think_news WHERE status = 1
Copy code
The default naming range 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,
- ),
- );
If you copy the code, you can directly call the default naming range:
- $ Model-> scope ()-> select ();
Copy the code without passing in the name range identifier
- $ Model-> scope ('default')-> select ();
Although the two methods are equivalent. If you need to add additional adjustments to the normal naming range, you can use:
- $ Model-> scope ('normal', array ('limit' => 5)-> select ();
The SQL statement generated by copying the code is:
- SELECT * FROM think_news WHERE status = 1 LIMIT 5
Of course, you can also adjust the replication code based on the two naming ranges, for example:
- $ Model-> scope ('normal, latest ', array ('limit' => 5)-> select ();
The SQL statement generated by copying the code is:
- SELECT * FROM think_news WHERE status = 1 order by create_time desc limit 5
Copy code
You can customize the naming range or simply not use any existing naming range. I will directly input a naming range:
- $ Model-> scope (array ('field' => 'id, title', 'limit' => 5, 'where' => 'status = 1 ', 'order' => 'Create _ time DESC ')-> select ();
Copy the code to convert the generated SQL statement:
- SELECT id, title FROM think_news WHERE status = 1 order by create_time desc limit 5
Copy code
A name range can be used in combination with a consistent operation. for example, a name range _ scope attribute is defined:
- Protected $ _ scope = array (
- 'Normal' => array (
- 'Where' => array ('status' => 1 ),
- 'Field' => 'id, title ',
- 'Limit' => 10,
- ),
- );
Copy the code and call it as follows:
- $ Model-> scope ('normal')-> limit (8)-> order ('Id desc')-> select ();
Copy the code to convert the generated SQL statement:
- SELECT id, title FROM think_news WHERE status = 1 order by id desc LIMIT 8
If the name range defined by the copy code conflicts with the attribute of the coherent operation, the subsequent calls overwrite the previous ones.
If so, call:
- $ Model-> limit (8)-> scope ('normal')-> order ('Id desc')-> select ();
The SQL statement generated by copying the code is:
- SELECT id, title FROM think_news WHERE status = 1 order by id desc LIMIT 10
Copy code
The advantage of the naming range function is that it can be defined multiple times at a time, and can also be used as a standard for division of labor in the project to avoid problems when developers write CURD operations, the project manager only needs to properly plan the name range.