problems using defaultScope

Hello my friends,

I am having some problems using defaultScope and thought of asking for an advise here…

So, I have 4 models - Media, Article, News, Content

in Media extends CActiveRecord I have


public function getNewsOptions() {

    $options = CHtml::listData(News::model()->findAll(), 'id', 'title');

    return $options;

}


public function getArticleOptions() {

    $options = CHtml::listData(Article::model()->findAll(), 'id', 'title');

    return $options;

}


class News extends Content

{    

    public function defaultScope() {

        return array(

            'condition'=>"t.type_id=:t_type_id",

            'params'=>array(':t_type_id'=>self::TYPE_NEWS),

        );

    }

    

    protected function beforeValidate() {

		if($this->isNewRecord) {

            $this->type_id = self::TYPE_NEWS;

        }

        return parent::beforeValidate();

    }

    


}



class Content extends CActiveRecord and is created by gii with


const TYPE_ARTICLE=0;

const TYPE_NEWS=1;

Inside MediaController.php I have


public function actionIndex()

{

    $news=Media::getNewsOptions();

    

	$dataProvider=new CActiveDataProvider('Media');

	$this->render('index',array(

		'dataProvider'=>$dataProvider,

		'news'=>$news,

	));

}

But, News::model()->findAll() returns all the Content rows, instead of returning Content rows with type_id=1

Any thoughts what I might be doing wrong?

I suggest you to write in the model this:




  public function getTypenews() {

    return $this->model()->findAll(new CDbCriteria(array(

        'select' => '*',

        'condition'=>"t.type_id=:t_type_id",

        'params'=>array(':t_type_id'=>self::TYPE_NEWS),

    )));

  }



And in the view:




News::model()->typenews



Yes, currently I am using a similar way to make it work without defaultScope.

And the weird thing is that in NewsController.php I have


public function actionIndex()

{

    $dataProvider=new CActiveDataProvider('News');

    $this->render('index',array(

            'dataProvider'=>$dataProvider,

    ));

}

And it shows correctly only Content rows with status_id=1,

so I thought I misunderstood the usage of defaultScope,

although here you can see that defaultScope also should be applied to News::model()->findAll()

Should I assume this is a bug or I misunderstood something about defaultScope?

http://code.google.com/p/yii/issues/detail?id=2019&sort=-id&colspec=ID%20Type%20Status%20Priority%20Milestone%20Owner%20Stars%20Summary

Hi! I have the same problem and solution that I found was to override parents static method model() like it was said in guide (Defining AR Class)! Hope it solves yours problem!