So I have two objects.
class Product extends DataMapper
{
var $has_many = array('productrating');
public $averageRating = null;
function __construct($id = NULL)
{
parent::__construct($id);
echo "my id: " . $this->id;
}
}
and
class ProductRating extends DataMapper
{
var $table = "product_ratings";
var $has_one = array('product');
function __construct($id = NULL)
{
parent::__construct($id);
}
}
What I would eventually like to accomplish is populate the Product->$averageRating
property with, you guessed it, the average rating based on the FK relationship with ProductRatings.
My first thought was to access the relationship, do the math based on the number of ProductRating rows and populate the property inside the constructor. But I'm having issues even accessing the object inside the constructor. This leads me to believe I'm probably going about this the wrong way. How can this be accomplished with DataMapper?
I know the math could be done in the View pages, but I would really like to keep this in the Model if possible.