Oze*_*ich 2 php activerecord criteria yii
我有一个模特:
class Service extends CActiveRecord
{
public static function model($className = __CLASS__)
{
return parent::model($className);
}
public static function getMainPageItems()
{
return self::model()->findAll(array(
'condition' => 'on_main = 1',
'order' => 'pos ASC'
));
public static function getNonMainPageItems()
{
return self::model()->findAll(array(
'condition' => 'on_main = 0',
'order' => 'pos ASC'
));
}
Run Code Online (Sandbox Code Playgroud)
我想将模型的默认顺序设置为pos ASC.
如何设置模型的默认顺序?
使用CActiveRecord::defaultScope()方法如下:
class Service extends CActiveRecord
{
...
public function defaultScope(){
return array(
'order'=>'pos ASC'
);
}
...
}
Run Code Online (Sandbox Code Playgroud)
这将添加到模型上的所有查找方法.阅读范围和defaultScopes以获取更多信息