I'm using CakePHP's tree behavior and need to know if there is any products in category or it's subcategories since I don't want to view empty categories.
I would like to do something like this:
$cat = $this->Category->find('first',array('conditions'=>array('id'=>$id)));
$test = $this->Category->find('threaded', array(
'conditions' => array(
开发者_如何学Go 'Category.lft >=' => $cat['Category']['lft'],
'Category.rght <=' => $cat['Category']['rght'],
'Product.InStock >'=>0 //NOT WORKING
)
));
That would be a starting point to unset not needed array dimensions. In database, categories hasMany products.
What could be best solution to this problem? Is it possible to avoid Product->find in foreach loop with category_id?
Untested
$this->Category->Behaviors->attach->('Containable');
$cat = $this->Category->find('first',array('conditions'=>array('id'=>$id)));
$test = $this->Category->find('threaded', array(
'conditions' => array(
'Category.lft >=' => $cat['Category']['lft'],
'Category.rght <=' => $cat['Category']['rght']),
// use containable behaviour and apply the condition
'contain'=>array('Product'=>array('conditions'=>
array('Product.InStock >'=> 0)
)
)
));
精彩评论