During application development, the most commonly used operations are data query operations. with the consistent ThinkPHP operations, query operations can be better and clearer. However, the new version of the naming range function, it defines a series of query encapsulation for model operations, making it easier to query data. During application development, the most commonly used operations are data query operations. with the consistent ThinkPHP operations, query operations can be better and clearer. However, the new version of the naming range function, it defines a series of query encapsulation for model operations, making it easier to query data.
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 of the currently defined name range.
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 naming range is called, each calling a naming range is equivalent to executing a consistent operation method corresponding to the operation options defined in the naming 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
Copy code
You can call multiple naming ranges at the same time. 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 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
In addition to using the scope method to call the name range, dynamic calling also supports calling the name of the name range directly. for example:
- $ Model-> scope ('normal', array ('limit' => 5)-> select ();
You can also perform the following operations to query the copied code:
- $ Model-> normal (array ('limit' => 5)-> select ();
Copy the code.
Normal (array ('limit '=> 5) indicates that the normal naming range is called and an additional array ('limit' => 5) parameter is input.
Because the _ call magic method mechanism is used, the premise for calling this method is that the name of the namescope you defined does not conflict with the existing operation method.
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.