Count unique combinations?

I need to find district combinations in a table, however, I also need to know how many times each of those occurred.

that is, I only want to display them once, but I do also need to know how many times those combinations exists.

this is for things that need to be ordered, and this particular part of the application should generate a list of what has to be ordered, but preferably in the same way you’d normally place the order.

so the list could be like this:

right now I have this:


$orders = OrderProducts::find()->select(['productId','sauceId'])->distinct()->all();

	if($orders != null){

		foreach($orders as $item){

				echo $item->product->name;

				if($item->sauce->id != 0){

				 echo ' '.$item->sauce->name;

				}

				echo '<br>';

		}

	}else{

		echo 'nothing to order.';

	}

but this lacks the amount of items, so in the example I gave above this would just say I need item 2 with mayo, but not that I need three of them.

Ideally I’d like to include the count of combinations in the same query, but so far I can’t get the the result at all, I did find examples that use count(), which makes sense since i do want to count something, only those just return how many combinations exists, not how many entries they represent.

so entries like this:


$counter = OrderProducts::find()->groupBy(['productId','sauceId'])->count();

but that only returns 4 (the example above it would return 5), but that is still how many groups exists, meaning I still don’t know how many of each things we need.

I though this would be pretty simple, but so far I can only find other examples that do the same as the code above, but they don’t count how many times each group exists in the database.

Maybe something like:




OrderProducts::find()

->select(['productId', 'sauceId', 'COUNT(*) AS cnt'])

->groupBy(['productId', 'sauceId'])

->all();




for some reason in that I don’t actually have ‘cnt’ in the results, if I var_dump the results I get an array of objects, but those objects only contain the attributes ‘productId’ and ‘sauceId’, but no ‘cnt’.

so:


$counter = OrderProducts::find()

->select(['productId', 'sauceId', 'COUNT(*) AS cnt'])

->groupBy(['productId', 'sauceId'])

->all();

var_dump($counter);exit;

results in:


array(4) {

[0]=> object(app\models\OrderProducts)#73 (<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> { ["_attributes":"yii\db\BaseActiveRecord":private]=> array(2) { ["productId"]=> string(1) "2" ["sauceId"]=> string(1) "3" } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(2) { ["productId"]=> string(1) "2" ["sauceId"]=> string(1) "3" } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(0) { } }

[1]=> object(app\models\OrderProducts)#81 (<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> { ["_attributes":"yii\db\BaseActiveRecord":private]=> array(2) { ["productId"]=> string(1) "2" ["sauceId"]=> string(1) "5" } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(2) { ["productId"]=> string(1) "2" ["sauceId"]=> string(1) "5" } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(0) { } }

[2]=> object(app\models\OrderProducts)#82 (<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> { ["_attributes":"yii\db\BaseActiveRecord":private]=> array(2) { ["productId"]=> string(1) "7" ["sauceId"]=> string(1) "5" } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(2) { ["productId"]=> string(1) "7" ["sauceId"]=> string(1) "5" } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(0) { } }

[3]=> object(app\models\OrderProducts)#83 (<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> { ["_attributes":"yii\db\BaseActiveRecord":private]=> array(2) { ["productId"]=> string(2) "60" ["sauceId"]=> string(1) "0" } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(2) { ["productId"]=> string(2) "60" ["sauceId"]=> string(1) "0" } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(0) { } }

} 

well, I found why, counter contains objects, which have attributes.

but only those that are actually defined in the class file…

…which $cnt wasn’t…


public $cnt;

in the Model class and now it works.

I clearly still need to get more used to working objective instead of procedural.