Wednesday, 15 January 2014

php - How to count and group by in yii2 -



php - How to count and group by in yii2 -

i generate next query using yii2:

select count(*) cnt lead approved = 1 grouping promoter_location_id, lead_type_id

i have tried:

$leadscount = lead::find() ->where('approved = 1') ->groupby(['promoter_location_id', 'lead_type_id']) ->count();

which generates query:

select count(*) (select * `lead` approved = 1 grouping `promoter_location_id`, `lead_type_id`) `c`

in yii 1.x would've done following:

$criteria = new cdbcriteria(); $criteria->select = 'count(*) cnt'; $criteria->group = array('promoter_location_id', 'lead_type_id');

thanks!

solution:

$leadscount = lead::find() ->select(['count(*) cnt']) ->where('approved = 1') ->groupby(['promoter_location_id', 'lead_type_id']) ->all();

and add together public $cnt model, in case lead.

as kshitiz stated, utilize yii\db\query::createcommand().

php mysql yii2

No comments:

Post a Comment