Reputation: 1435
I'm trying to count the number of records. We have looked at it for days, and the count isn't correct. It keeps returning 1 when there's nothing. All the other models' count return correctly, so this one is quite bizzare. Any other places I need to check?
Things already looked at:
$pending = count(Model::model()->pending());
Rules are safe in model. Model:
public function pending()
{
$criteria = new CDbCriteria;
$now = new CDbExpression("NOW()");
$criteria->addCondition('effective_to_date > '.$now);
$criteria->addCondition('effective_from_date < '.$now);
$criteria->compare('target_accept',"ACCEPTED");
return new CActiveDataProvider($this, array( 'criteria'=>$criteria, ));
}
Upvotes: 1
Views: 87
Reputation: 2116
What you are doing is that you are returning CActiveDataProvider
instance which itself does not contain the data rather you can get list of objects using method getData().
You can write like this
public function pending()
{
$criteria = new CDbCriteria;
$now = new CDbExpression("NOW()");
$criteria->addCondition('effective_to_date > '.$now);
$criteria->addCondition('effective_from_date < '.$now);
$criteria->compare('target_accept',"ACCEPTED");
$dataProvider=new CActiveDataProvider($this, array( 'criteria'=>$criteria, ));
return $dataProvider->getData();
}
Also you can use property totalItemCount
like $dataProvider->totalItemCount
to get total count of records. and then you can directly use this count in your lvalue like
$pending=Model::model()->pending()
Upvotes: 1
Reputation: 8838
Try this code:
$dp = Model::model()->pending();
echo count($dp->getData())
Upvotes: 2