ThinkPHP3.1 the use of the naming range for new features _php tutorial

Source: Internet
Author: User
Tags ranges
The ThinkPHP3.1 naming range feature provides a series of (coherent operation) packages for model operations, allowing you to query and manipulate data more easily. Let's take a look at this usage in detail below.

1. Defining attributes

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:

Class Newsmodel extends Model {  protected $_scope = Array (    //naming range normal    ' normal ' =>array (      ' WHERE ' = >array (' status ' =>1),    ),    //Name 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 ',  ...)

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 condition
field query field
order result sort
table query table name
limit result limit
page results page
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.

3. 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 = D (' News '); The D method must be used here because the naming range defines $model->scope (' normal ')->select () in the model, $Model->scope (' latest ')->select ();

The resulting SQL statements are:

SELECT * FROM Think_news WHERE status=1select * from Think_news ORDER by Create_time DESC LIMIT 10

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 Think_news WHERE status=1 ORDER by Create_time DESC LIMIT 10

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


4. Default naming Range

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

Protected $_scope = Array (    //default named 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 Think_news WHERE Status=1 LIMIT 5

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 Think_news WHERE status=1 ORDER by Create_time DESC LIMIT 5


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 id,title from Think_news WHERE status=1 ORDER by Create_time DESC LIMIT 5


5. Mixed use with coherent operation

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 id,title from Think_news WHERE status=1 ORDER by id desc LIMIT 8

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:

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

The resulting SQL would be:

SELECT id,title from Think_news WHERE status=1 ORDER by id desc LIMIT 10


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.

http://www.bkjia.com/PHPjc/825371.html www.bkjia.com true http://www.bkjia.com/PHPjc/825371.html techarticle the ThinkPHP3.1 naming range feature provides a series of (coherent operation) packages for model operations, allowing you to query and manipulate data more easily. Below we will be specific to understand the use of ...

  • 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.