Use of ThinkPHP3.1 new features in the naming range

Source: Internet
Author: User
Tags custom name
The naming range function of ThinkPHP3.1 provides a series of (consistent operations) encapsulation for model operations, allowing you to conveniently query and operate data. This article mainly introduces the use of ThinkPHP3.1 naming range. For more information about ThinkPHP, see ThinkPHP.

The naming range function of ThinkPHP3.1 provides a series of (consistent operations) encapsulation for model operations, allowing you to conveniently query and operate data. Next, let's take a look at this usage.

1. define attributes

To use the naming range function, we mainly need to define the _ scope attribute of the model class and use the coherent operations of the scope.
First, we define the _ scope attribute:

Class NewsModel extends Model {protected $ _ scope = array (// name range: normal 'normal' => array ('where' => array ('status' => 1 ),), // name range: latest 'latest' => array ('order' => 'Create _ time DESC ', 'Limit' => 10 ,),);}

The _ scope attribute is an array. each array entry 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 ',...)

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

3. method call

After the attribute is defined, the scope method is used to call the naming range. each call to a naming range is equivalent to executing the related operation options defined in the naming range.

Call a naming range

The simplest call method is to directly call a specified name range, for example:

$ Model = D ('New'); // The D method must be used here because the name range defines $ Model-> scope ('normal')-> select () in the Model (); $ Model-> scope ('latest ')-> select ();

The generated SQL statements are:

SELECT * FROM think_news WHERE status=1SELECT * FROM think_news ORDER BY create_time DESC LIMIT 10

Call multiple naming ranges

You can also call multiple naming ranges at the same time, for example:

$Model->scope('normal')->scope('latest')->select();

Or simplified:

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

The generated SQL statements are:

SELECT * FROM think_news WHERE status=1 ORDER BY create_time DESC LIMIT 10

If the definitions of the two naming ranges conflict, the naming range definitions called later will overwrite the previous definitions 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 clause in the above naming range does not exist, so only the normal naming range takes effect. the generated SQL statement is:

SELECT * FROM think_news WHERE status=1


4. default naming range

The 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 ,),);

You can directly call the default naming range:

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

Instead of passing in the name range identifier

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

Although these two methods are equivalent.

Adjust the name range

If you need to add additional adjustments based on the normal naming range, you can use:

$Model->scope('normal',array('limit'=>5))->select();

The generated SQL statement is:

SELECT * FROM think_news WHERE status=1 LIMIT 5

Of course, you can also adjust the two naming ranges, for example:

$Model->scope('normal,latest',array('limit'=>5))->select();

The generated SQL statement is:

SELECT * FROM think_news WHERE status=1 ORDER BY create_time DESC LIMIT 5


Custom name range

Alternatively, you do not need to use any existing naming ranges. I directly input a naming range:

$Model->scope(array('field'=>'id,title','limit'=>5,'where'=>'status=1','order'=>'create_time DESC'))->select();

In this way, the generated SQL becomes:

SELECT id,title FROM think_news WHERE status=1 ORDER BY create_time DESC LIMIT 5


5. mixed use with coherent operations

The same name range can be used in combination with the previous coherent operation. for example, the name_scope attribute is defined:

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

Then, you can call the following method when using it:

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

In this way, the generated SQL becomes:

SELECT id,title FROM think_news WHERE status=1 ORDER BY id desc LIMIT 8

If the defined naming range conflicts with the attribute of a coherent operation, the subsequent call overwrites the previous one.
If so, call:

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

The generated SQL statement is:

SELECT id,title FROM think_news WHERE status=1 ORDER BY id desc LIMIT 10


Summary
The advantage of the naming range function is that you can define multiple calls at a time.In addition, the project can also adopt a division of labor to avoid problems when developers write CURD operations. the project manager only needs to properly plan the naming scope.

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.