I'm sure I'm missing something as this seems like something that should be simple to do using the nested_set
behavior.
I have listings which sit in multiple categories. The categories are managed using the nested_set
behavior in Propel. The categories also have an aggregate_column
called listing_count
which is used to count the listings in each category.
For each individual category this works perfectly. What I am having trouble with is the accumulating the totals for each ancestor of the category.
For example the 'Sport' category has 3 children called 'Golf', 'Football' and 'Tennis'. The 'Tennis' category has 2 children called 'Grass' and 'Clay'. When a listing is added to 'Clay', I need the totals for 'Sport' and 'Tennis' to also update.
I can't figure out a (nice) way of accomplishing this. Can anyone shed any light?
Maybe following the documentation (not tested) :
// increase listing_count counter for ancestors of Clay
$path = $clay->getAncestors();
foreach($path as $node)
{
$node->setListingCount($node->getListingCount()+1);
$node->save();
}