且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 PHP 中对此类属性求和,以便获得所有类实例的总值?

更新时间: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