Abou cont on Active Record

Hello friends

I some cases I need return only a count, so to do it I use something like:




$count = MyModel::find()->where(['field'=>'string'])->count();



Works very well, but I need to put a limit like:




$count = MyModel::find()->where(['field'=>'string'])->limit(20)->count();



Dont works, give result but if exist more than 20 lines, the result bring count of all lines

So I do it:




$count = MyModel::find()->where(['field'=>'string'])->limit(20)->all();

$x = count($count);



But it bring me a array of data and I want only de count value.

There is a way to use ‘…->limit(20)->count()’ ?;

There is no simple way for it: http://stackoverflow.com/questions/17020842/mysql-count-with-limit

But you may use this code:





$count = min(MyModel::find()->where(['field'=>'string'])->count(), 20);