Yii计算模型中的记录

In my web application I want to print the no of records for a particular criteria . But the output I am getting is 0 , although there are many records for the matching condition. This code is inside a view of another model . "$data" belongs to that model

My code I implemented.

$temp=CHtml::encode($data->name);

        $find=ConsumerRequest::model()->findAllByAttributes(array('requested_vegetable'=>$temp));
        $count = count($find);
       echo $count;

How should I resolve this?

You can try these methods:

$temp=CHtml::encode($data->name);
$count = ConsumerRequest::model()->findAll(array("condition"=>"requested_vegetable = '$temp'"));
$count = count($find);
echo $count;   

OR

$temp=CHtml::encode($data->name);
$Criteria = new CDbCriteria();
$Criteria->condition = "requested_vegetable = '$temp'";
$count = ConsumerRequest::model()->findAll($Criteria);
$count = count($find);
echo $count; 

I wouldn't use the findAll() functions as this pulls over all the data for each row. You can simply use the count() or countByAttributes() function. I would guess your problem is probably this line:

$temp=CHtml::encode($data->name);

Most likely you are not storing it in the database HTML encoded. Try just doing this:

$count = ConsumerRequest::model()->countByAttributes(array('requested_vegetable'=>$data->name));

You can also do like this: $query='SELECT COUNT(id.tbl) FROM tbl'; CMIIW

Try adding public $count on your Model right after the class syntax. And use CDbCriteria to make a custome query.