ThinkPHP3.1 new features: Named ranges

Source: Internet
Author: User

Overview

The naming range feature provides a series of (coherent) encapsulation of model operations, making it easier to query and manipulate data. Let's take a concrete look at this usage.

Defining properties

To use the naming range feature, the _scope attribute definition of the model class and the use of the scope coherent operation method are mainly involved.
We first define the _scope property:

classNewsmodelextendsModel {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, each array item represents a named range defined, and the named range is defined in the following format:

' Named range id ' = =array    (' attribute 1 ' = ' = ' value 1 ',    ' attribute 2 ' = ' = ' value 2 ',    ...  )

Named range ID : can be any string that identifies the currently defined named range.
The properties supported by a named range include:

where Query criteria
Field Query fields
Order Result sort
Table Query table name
Limit Result limits
Page Results page out
Having Having query
Group Group Query
Lock Query lock
Distinct Unique Query
Cache Query cache

The definition of each named range can include one or more of these attributes.

Method invocation

Once the property definition is complete, the next step is to use the scope method to make a named range call, and each invocation of a named range is equivalent to performing the related operation options defined in the named range.

Call a named range

The simplest invocation method calls a named range directly, for example:

$Model // The D method must be used here because the named range is defined inside the model $Model->scope (' normal '),Select (); $Model->scope (' latest ')->select ();

The resulting SQL statements are:

SELECT *  from WHERE Status=1SELECT*fromORDER by DESC
Calling multiple named ranges

You can also support invoking multiple named range definitions at the same time, for example:

$Model, scope (' normal'), scope ('latest ')  - Select ();

or simplified to:

$Model, scope ('normal,latest'),select ();

The resulting SQL is:

SELECT *  from WHERE Status=1ORDERbyDESC

If there is a conflict in the definition of two named ranges, then the named range definition that is called later overrides the previous definition of the same property.
If the named range ID of the call does not exist, the named range is ignored, for example:

$Model->scope (' normal,new ')->select ();

New in the above named range does not exist, so only the normal naming range is valid, and the resulting SQL statement is:

SELECT * from Think_news WHERE Status=1
Default naming Range

The system supports the default naming range feature, if you have defined a name range, for example:

    protected $_scope Array (        //  default naming range        ' default ' = =array(            ' where ' =  Array(' status ' =>1),            ' limit ' =>10,        )    ;

Then calling the default named range can be used directly:

$Model->scope ()->select ();

Without having to pass in a named range identity name

$Model->scope (' Default ')->select ();

Although the two methods are equivalent.

Named 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 WHERE Status=15

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 WHERE Status=1ORDERbyDESC5
Custom Named ranges

Or, simply without any existing naming ranges, I pass directly to 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  from WHERE Status=1ORDERbyDESC5
Mixed use with coherent operations

Named scopes can be mixed with previous coherent operations, such as defining a named range _scope property:

protected $_scope Array (    ' normal ' = =array(        ' where ' = =array(' status ' =>1),        ' Field ' = ' id,title ',        ' limit ' =>10,    ) ;

Then, when used, this can be called:

$Model->scope (' normal ')->limit (8)->order (' id desc ')->select ();

In this way, the resulting SQL becomes:

SELECT  from WHERE Status=1ORDERbydesc8

If there is a conflict between the defined naming range and the properties of the coherent operation, the previous call is overwritten.
If this is called:

Scope('normal'), $Model, limit (8) Order ('ID desc')  - Select ();

The resulting SQL would be:

SELECT  from WHERE Status=1ORDERbydesc
Summarize

The advantage of the naming range feature is that multiple invocations can be defined at once, and the specification of the Division of Labor can also be played in the project, avoiding the problem of developers writing curd operations, and the project manager only needs to plan the naming range reasonably.

Via:http://www.thinkphp.cn/document/104.html

ThinkPHP3.1 new features: Named ranges

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.