Yii framwork crud named range NAMED SCOPE_PHP tutorial

WBOY
Release: 2016-07-14 10:10:06
Original
860 people have browsed it

Named range
Note: Support for named ranges starts with version 1.0.5. The original idea for named scopes came from Ruby on Rails.
A named scope represents a named query rule, which can be used in conjunction with other named scopes and applied to Active Record queries
Named scopes are mainly declared in the form of name-rule pairs in the CActiveRecord::scopes() method. The following code declares two named ranges in the Post model class,
published and recently.
class Post extends CActiveRecord
{
 …
Public function scopes()
{
         return array(
            'published'=>array(
                'condition'=>'status=1',
),
            'recently'=>array(
                'order'=>'create_time DESC',
                'limit'=>5,
),
);
}
}
Specific usage: $posts=Post::model()->published()->recently()->findAll();
Note: Named scopes can only be used with class-level methods. That is, this method must be called using ClassName::model().

Parameterized named range
Named ranges can be parameterized. For example, we want to customize the number of posts specified in the recently named scope, to achieve this, not in CActiveRecord::scopes
Instead of declaring a named range in a method, you need to define a method with the same name as the named 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 get the 3 most recent posts.
$posts=Post::model()->published()->recently(3)->findAll();//In the above code, the 5 most recently published posts are obtained by default.

Default scope
Model classes can have a default scope that will apply to all queries (including related ones) about this model. For example, a website that supports multiple languages ​​may only want to display
Displays content in the language specified by the current user. Because there may be many queries about the content of this website, we can define a default scope to solve this problem. For real
For this purpose, we override the CActiveRecord::defaultScope method 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 rules defined above will be automatically used: $contents=Content::model()->findAll();


Practical applications
The above are several official examples. Here are the examples I use myself 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: The query is for all classes with school "a". At this time, we can first define a relationship in the relations method in the School model to facilitate the call
public function relations()
{
Return array(
         'class'=>array(self::HAS_MANY, '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();

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477592.htmlTechArticleNamed range Note: Support for named ranges starts from version 1.0.5. The original idea of ​​named scope comes from Ruby on Rails. A named scope represents a named query...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!