Yiiframworkcrud name range NAMEDSCOPE_PHP tutorial

Source: Internet
Author: User
Tags ruby on rails
Yiiframworkcrud: NAMEDSCOPE. Naming range Note: Supports the naming range from version 1.0.5. The original idea of naming ranges comes from RubyonRails. namedscope indicates a named (named) query naming range.
Note: The naming range is supported starting from version 1.0.5. The original idea of naming scope came from Ruby on Rails.
Named scope indicates a named (named) query rule, which can be used together with other naming ranges and applied to Active Record queries.
The naming range is declared by name-rule pair in the CActiveRecord: scopes () method. The following code declares two naming ranges in the Post model class,
Published and recently.
Class Post extends CActiveRecord
{
......
Public function scopes ()
{
Return array (
'Published' => array (
'Condition' => 'status = 1 ',
),
'Cently '=> array (
'Order' => 'Create _ time DESC ',
'Limit' => 5,
),
);
}
}
Usage: $ posts = Post: model ()-> published ()-> recently ()-> findAll ();
Note: The naming range can only be used for class-level methods. That is to say, this method must be called using ClassName: model.

Parameterized naming range
The naming range can be parameterized. For example, if we want to customize the number of posts specified in the recently naming range, we do not want to achieve this purpose in CActiveRecord: scopes
To declare the name range, you must define a method with the same name as the name of the naming range:
Public function recently ($ limit = 5)
{
$ This-> getDbCriteria ()-> mergeWith (array (
'Order' => 'Create _ time DESC ',
'Limit' => $ limit,
));
Return $ this;
}
Then, we can use the following statement to obtain three recently published posts.
$ Posts = Post: model ()-> published ()-> recently (3)-> findAll (); // In the above code, by default, five recent posts are obtained.

Default range
A model class can have a default range, which will be applied to all (including related) queries about this model. For example, a website that supports multiple languages may only want to display
Displays the content of the language specified by the current user. Because there may be a lot of queries on the content of this website, we can define a default range to solve this problem. True
For this purpose, the method of overwriting CActiveRecord: defaultScope is as follows:
Class Content extends CActiveRecord
{
Public function defaultScope ()
{
Return array (
'Condition' => "language = '". Yii: app ()-> language ."'",
);
}
}

Now, if the following method is called, the query rule defined above: $ contents = Content: model ()-> findAll ();


Practical application
The above are some official examples. below are my own examples for your reference:

TABLE:
School: id (pk, int), name (varchar) // school
Class: id (pk, int), name (varchar), s_id (fk, int) // class
Student: id (pk, int), name (varchar), s_id (int), c_id (int) // student

Example: if the query type is "a" for all the classes, we can first define a link in the relations method of the School model for convenient calling.
Public function relations ()
{
Return array (
'Class' => array (self: has_example, 'class','s _ id '),
);
}
Public function scopes ()
{
Return array (
'Getallclass' => array (
'With' => 'class ',
),
};
}
Public function getAllClass ($ name)
{
$ This-> getDbCriteria ()-> mergeWith (array (
'Condition' => "t. name = $ name ",
));
Return $ this;

}

Use: School: model ()-> getAllClass ("a")-> findAll ();

Release Note: Supports the naming range from version 1.0.5. The original idea of naming ranges came from Ruby on Rails. named scope indicates a named (named) query...

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.