I am using the PHP MVC framework, Yii. I have a model called Category that has a HAS_MANY relationship with the Product model. Both model classes extend CActiveRecord .
I am looking for help in understanding some of the specific ways that OOP works in PHP.
In my view for my category model, I am trying to find the total inventory for a category. For example, I could do this:
<?php $total_inventory = 0; foreach($category->products as $product) $total_inventory = $total_inventory + $product->inventory; echo $total_inventory; ?>
Why can't I do this?
<?php echo array_sum($category->products->inventory); ?>
This results in an error: PHP Notice: Trying to get property of non-object .
Then I thought the following might work:
<?php echo array_sum($category->products->getAttribute('inventory')); ?>
However, this results in: Fatal error: Call to a member function getAttribute() on a non-object .
Is it possible to use array_sum() as I try, just to change something, or is it impossible, because $category->products is actually an object, not an array? Is it correct?
I am glad that I was provided with articles or documentation that will help explain what I am missing.
thanks
source share