Ok, so I had such models: Product (product_id) Order (order_id, product_id) OrderProduct (order_id, product_id)
Product model had relation
'orders'=>array(self::HAS_MANY,'Order',array('order_id'=>'order_id'),'through'=>'orderedProducts'),
My task was - to count each product->orders, without loading all orders models.
As Ross suggested, I created STAT relation
'statOrders'=>array(self::STAT,'Order',array('order_id'=>'order_id'),'through'=>'orderedProducts'),
Eventually when I called $product->statOrders, I got an exception CStatRelation has no 'through' attribute
Which means that if I want to make it work, I have to invent another way to describe relation between products and orders.. It's even worse than duplication.
Finally I came up with this workaround:
$c=$product->getRelated('orders', true, аггау('select'=>'CОUNТ(огdегѕ.огdег_іd) as order_id', 'gгоuр'=>'ргоduсt_іd'));
$count=$c[О]->order_id;
• No need to describe new relation
• It makes clean sql query:
system.db.CDbCommand.query(SELECT COUNT(orders.order_id) as order_id, `orders`.`order_id` AS `t1_c0` FROM `orders` `orders` LEFT OUTER JOIN `orders_products` `orderedProducts` ON (`orderedProducts`.`order_id`=`orders`.`order_id`) WHERE (`orderedProducts`.`product_id`=:ypl0) GROUP BY product_id. Bound with :ypl0='458739')
• You can dynamically add params:
• By analogy you can count amount of products in orders (sum products quantity in each order):
My current final solution: