ThinkPHP3.1 quick start (14) name range

Source: Internet
Author: User
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:
  1. Class NewsModel extends Model {
  2. Protected $ _ scope = array (
  3. // The name range is normal.
  4. 'Normal' => array (
  5. 'Where' => array ('status' => 1 ),
  6. ),
  7. // Name range: latest
  8. 'Latest '=> array (
  9. 'Order' => 'Create _ time DESC ',
  10. 'Limit' => 10,
  11. ),
  12. );
  13. }
The copy code _ scope attribute is an array. each array item defines a naming range. the format of the naming range is as follows:
  1. 'Name range identifi' => array (
  2. 'Property 1' => 'value 1 ',
  3. 'Property 2' => 'value 2 ',
  4. ...
  5. )
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:
  1. $ Model = D ('New'); // The D method must be used here because the name range is defined in the Model.
  2. $ Model-> scope ('normal')-> select ();
  3. $ Model-> scope ('latest ')-> select ();
The SQL statements generated by the copied code are:
  1. SELECT * FROM think_news WHERE status = 1
  2. 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:
  1. $ Model-> scope ('normal')-> scope ('latest ')-> select ();
Copy the code or simplify it:
  1. $ Model-> scope ('normal, latest ')-> select ();
The SQL statements generated by the copied code are:
  1. 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:
  1. $ 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:
  1. 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:
  1. Protected $ _ scope = array (
  2. // Default name range
  3. 'Default' => array (
  4. 'Where' => array ('status' => 1 ),
  5. 'Limit' => 10,
  6. ),
  7. );
If you copy the code, you can directly call the default naming range:
  1. $ Model-> scope ()-> select ();
Copy the code without passing in the name range identifier
  1. $ 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:
  1. $ Model-> scope ('normal', array ('limit' => 5)-> select ();
The SQL statement generated by copying the code is:
  1. 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:
  1. $ Model-> scope ('normal, latest ', array ('limit' => 5)-> select ();
The SQL statement generated by copying the code is:
  1. 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:
  1. $ 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:
  1. 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:
  1. Protected $ _ scope = array (
  2. 'Normal' => array (
  3. 'Where' => array ('status' => 1 ),
  4. 'Field' => 'id, title ',
  5. 'Limit' => 10,
  6. ),
  7. );
Copy the code and call it as follows:
  1. $ Model-> scope ('normal')-> limit (8)-> order ('Id desc')-> select ();
Copy the code to convert the generated SQL statement:
  1. 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:
  1. $ Model-> limit (8)-> scope ('normal')-> order ('Id desc')-> select ();
The SQL statement generated by copying the code is:
  1. 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:
  1. $ Model-> scope ('normal', array ('limit' => 5)-> select ();
You can also perform the following operations to query the copied code:
  1. $ 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.