Cactivedataprovider Count And Sum. Please Help


I want relation to order count.

Table example






-customer_idcustomer (relations)





$dataCustomer =new CActiveDataProvider('Customer');


I want to

Customer name | COUNT Order | Total

 ikizmert    |    2        | 546.00

 micheal     |    10       | 786.00

Customer is how moch order total . (SUM)

Customer is how much count order… (Count)

I will hope to understand, my english easy.

Thank you…

Hi ikizmert,

You can use the statistical query.

Also look up CActiveRecord.relations in the reference.

In your order model make a relation like this:


I can total order, but i cant order by totalcount.

And how can ı order total.My english is insufficient

Unfortunately the STAT relations don’t work for filtering and sorting.

You have to use sub-queries for that.

Please have a look at this:

Okay bro thank you…

İ will looking…

I can filtering this function.


    function array_msort($array, $cols)


        $colarr = array();

        foreach ($cols as $col => $order) {

            $colarr[$col] = array();

            foreach ($array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); }


        $eval = 'array_multisort(';

        foreach ($cols as $col => $order) {

            $eval .= '$colarr[\''.$col.'\'],'.$order.',';


        $eval = substr($eval,0,-1).');';


        $ret = array();

        foreach ($colarr as $col => $arr) {

            foreach ($arr as $k => $v) {

                $k = substr($k,1);

                if (!isset($ret[$k])) $ret[$k] = $array[$k];

                $ret[$k][$col] = $array[$k][$col];



        return $ret;



Used :

$sipariSayisi = Musteriler::model()->with('siparisCount','itemsTotal')->findAll();

$siparisSayisinaGore = array_msort($sipariSayisi, array('siparisCount'=>SORT_DESC));

PHP array you get by "findAll" can be very large sometimes … your solution will work fine for a small data, but may have problems for a large data.

IMHO, it would be far better to filter/sort the result in SQL level, not in PHP array level. :)