[Solved] CGridView a fixed condition

hello all,

I have problem regarding displaying data via CGridView. I want to display records that meet certain condition. This condition is a fixed, so for example I have table like this

Product

===========

id

name

price

stock

category_id

I would like to show all records that the category_id value is 10. Where do I define the property? I ever try to do like this. But it doesn’t work :(




<?php 

$model = Inventory::model();

$model->category_id=10;

$dataProvider = new CActiveDataProvider(get_class($model));

$this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'inventory-grid',

	'filter'=> $model,

	'dataProvider'=>$dataProvider,

	'columns'=>array(

		'id',

		'name',

                'price',		

	)

)

); ?>

Any idea ?

I think I have got the answer myself. Since I am newbie, I’ve totally forgotten there is CDbCriteria!! :D

So later I change the code into this, and I got the result that I want




<?php 

$model = Inventory::model();


$criteria=new CDbCriteria;

$criteria->compare('category_id',10);


$dataProvider = new CActiveDataProvider(get_class($model),array('criteria'=>$criteria));

$this->widget('zii.widgets.grid.CGridView', array(

        'id'=>'inventory-grid',

        'filter'=> $model,

        'dataProvider'=>$dataProvider,

        'columns'=>array(

                'id',

                'name',

                'price',                

        )

)

); ?>



that solve the problem. So, I just need to set the fix valued in the $criteria. That’s all~