I have a User model and an Order model. User hasMany Order.
订单表有用户栏和总栏。
我希望在用户模式上有一个<代码>virtul Field,即有多少订单与用户有联系。
I would also like to have a virtualField on the User that is the sum of all the totals.
我能够做到这一点的唯一办法就是:
public $virtualFields = array(
total_orders => (SELECT COUNT(orders.user_id) FROM orders WHERE orders.user_id = User.id AND orders.status != "void" GROUP BY orders.user_id) ,
total_sales => (SELECT SUM(orders.total) FROM orders WHERE orders.user_id = User.id AND orders.status != "void" GROUP BY orders.user_id) ,
);
我不清楚,这是否是最佳解决办法,但是否有更好的办法?