I've searched for a solution to my specific problem, but haven't found it yet - probably because I'm not sure how to describe it in a search field ;)
The situation is as follows: Model 'Product' hasmany 'Variation' (as in: a product in a webshop can come in different colors). Each Variation has it's own stock and price. For sorting purposes, I'd really like to use the beforeSave and afterSave methods of both models to update the Product's fields 'min_price' and 'max_price'. This is needed, because I want to present products in a category sorted by lowest Variation price ASC and then name ASC. Using Set::sort doesn't cut it, because that won't allow me to do a multisort (for min_price and then name). My first guess was to use the afterSave method from the Variation to get the lowest price for any variation and save that to the min_price field of the Product. Same for max_price, obviously. If I would do the same for the afterDelete method, I thought I could count on the Product's field min_price and max_price to reflect the proper value at any time, allowing me to use the neede sorting on Model::find. However, there seems to be a delay between saving and retrieving the needed values. Using the simplified query below (executed in the afterSave method of the variation), the query lags in retrieving the proper, just changed, value for min_price and max_price: $sql = "SELECT MIN(price) AS min_price, MAX(price) AS max_price FROM variations WHERE product_id = $productId"; Funny thing is, changing the price of a variation does not give me a new min_price or max_price until after I save again. It is as if the save hasn't finished yet, while I try to retrieve the data. Looking at the database, I see the changes to the Variation model have been saved, but the results from my query in the afterSave method suggest otherwise. To be honest: we're talking about a 1.1 application here, I'm not sure if this behaviour would occur in a more recent version of Cake. Any ideas? I'm probably overlooking some important design principle or something, but I can't think of any other way of doing this... Thanks a bunch, Jeroen 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 [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en To unsubscribe, reply using "remove me" as the subject.
