Reputation: 2000
I have a model:
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'
));
}
I want to set the model's default order to pos ASC
.
How can I set the model's default order?
Upvotes: 2
Views: 2825
Reputation: 14860
Use CActiveRecord::defaultScope()
method as follows:
class Service extends CActiveRecord
{
...
public function defaultScope(){
return array(
'order'=>'pos ASC'
);
}
...
}
This will be added for all find methods on the model. Read on scopes and defaultScopes for more information
Upvotes: 8
Reputation: 2338
One way to go about this is to add a private member to the parent class called $order.
You would need to create getters and setters for it (so that you can change the defalut when you want)
Then simply call $this->$order
in the 'order' element in each function that requires it.
Upvotes: 0