1

我正在使用 CakePHP 的树行为,并且需要知道类别中是否有任何产品或其子类别,因为我不想查看空类别。

我想做这样的事情:

$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'],
            'Product.InStock >'=>0 //NOT WORKING
        )
    ));

这将是取消设置不需要的数组维度的起点。在数据库中,分类有很多产品。

这个问题的最佳解决方案是什么?是否可以使用 category_id 在 foreach 循环中避免 Product->find?

4

1 回答 1

1

未经测试

$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)
                                     )
                         )
        ));
于 2011-07-05T09:31:34.483 回答