hammies
hammies

Reputation: 1435

return incorrect count on activerecords

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:

  1. var_dump: so it returns 0; everything as expected;
  2. check scope if there's some kind of conflict;

$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

Answers (2)

Rafay Zia Mir
Rafay Zia Mir

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

Kumar V
Kumar V

Reputation: 8838

Try this code:

$dp = Model::model()->pending();
echo count($dp->getData())

Upvotes: 2

Related Questions