CActiveRecord::count() returns a string

CActiveRecord::count() should always return an integer. Please, someone tell me that i’m overlooking something. But i was very suprised today when i found this out today:

$count = User::model()->count();

echo gettype($count);	// echoes 'string'

echo ($count===0) ? 'No users' : 'We have users'; // will not work as expected

I thought, this already worked for me. But checked with an older version, too. It’s the same there.

Worth a report in the tracker:

Done. Just wanted to make sure, i’m not missing anything.

PDO does return strings to make sure integers don’t lose precision.


It’s a pain I know. Just use == when comparing data from db.

That’s not the problem here. A fix is still easy:



    return (int) $builder->createCountCommand($this->getTableSchema(),$criteria)->queryScalar();



    $finder=new CActiveFinder($this,$criteria->with);

    return (int)$finder->count($criteria);


… same for all other count methods.


Now i read your post again - o.k. we might limit the range for max count values this way. I’m still for a change. If not, at least it needs to be made clearer in the API docs.

Not only counts, but also for columns (e.g. INT UNSIGNED).

Maybe we can add some info on this page. You have better idea? Not sure if we should add this under every affected method (in the API docs).

Return type "int" is simply wrong. So this needs to be corrected - you can spend lot of time to find bugs like these in your code, that could have been avoided with correct API docs.

Like i’ve written in the issue: A small note on the return value should help to clarify why it’s a string.