ikizmert
(Ikizmert)
1
Hello,
I want relation to order count.
Table example
Orders
-------
-idorder
-number
-total
-customer_idcustomer (relations)
Customer
--------
-idcustomer
-customer_name
$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…
softark
(Softark)
2
shgninc
(Shgninc)
3
In your order model make a relation like this:
'customer_count'=>array(self::STAT,'Customer','idcustomer',),
ikizmert
(Ikizmert)
4
I can total order, but i cant order by totalcount.
And how can ı order total.My english is insufficient
softark
(Softark)
5
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:
http://www.yiiframework.com/wiki/319/searching-and-sorting-by-count-of-related-items-in-cgridview
ikizmert
(Ikizmert)
7
I can filtering this function.
<?php
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).');';
eval($eval);
$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));
softark
(Softark)
8
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.