Thursday, December 17, 2009

Working around Cake's array approach to Models

Good morning,

I am looking for the best way to implement a fairly simple object
oriented problem in CakePHP.

To summarize I have an object that must depend on a collection of
other objects to calculate a value that must be displayed to the user
as well as used in other calculations throughout the system. A
pseudocode example is below:

interface IPriceModifier
{
public function getModifiedPrice($product, $total);
}

class AddADollarModifier implements IPriceModifier
{
public function getModifiedPrice($product, $total) {
return $total + 1;
}
}

class Product
{
private $priceModifiers = array();

public function getPrice() {
$total = 0;
for ($this->priceModifiers as $modifier) {
$total = $modifier->getModifiedPrice($this, $total);
}

return $total;
}

public function addPriceModifier($priceMod) {
$this->priceModifiers[] = $priceMod;
}
}

To get the price for a product it must loop through a collection of
price modifiers that are decoupled from Product. They can come from
any source, internal code, or code from vendors that hasn't been
written yet. Also notice that in many cases there will be
calculations happening based on the product, so each one will be
calculating values via a variety of methods and can not be predicted.

My initial try on this problem involved dynamically adding related
models to the product model. I would then process the related model
data via afterFind in the product model and let cake call the attached
specialized behaviors that handle each of the concrete types of
IPriceModifiers associated with the current product. These will
filter their type of discount and add a simple price adjustment that
the Product model could read to figure out it's price and set a field
on itself in it's own afterFind. From what I saw this was the
recommended way of dealing with this issue.

This was working until I ran into an issue with afterFind not being
called in behaviors of nested models. I tried a few patches and
couldn't get anything to work. This poses a problem because the
product object will be used by many other models in the system.

There are a few places where this pattern will be used and I would
hate to have to call all the models separately and then link up the
relations myself because that defeats the largest reason to use
CakePHP.

This is a fairly standard problem that OO solves rather eloquently..
What is the best way to do this in CakePHP?

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate