更新时间:2023-11-24 22:41:22
实际上 $category->products
是一个对象数组.这就是你不能在对象属性上使用 array_sum
的原因.您可以通过在 类别
型号
Actually the $category->products
is an array of objects. Thats why you cant use array_sum
on the object attributes. You can get the sum the easy way by adding a STAT relation in the Category
Model
'total_inventory'=>array(self::STAT, 'Product', 'cat_id','select'=>'SUM(inventory)')
并且您可以通过调用 $category->total_inventory