想在cakephp 3中获得子类别产品吗
我有树状结构的分类表和产品表。我想在列表中显示。当用户单击父类别时,我希望显示该类别的所有产品以及该类别的子类别的产品想在cakephp 3中获得子类别产品吗,php,mysql,cakephp,Php,Mysql,Cakephp,我有树状结构的分类表和产品表。我想在列表中显示。当用户单击父类别时,我希望显示该类别的所有产品以及该类别的子类别的产品 public function listing($catid = null, $slug = null) { $this->viewBuilder()->layout('frontview'); $this->loadModel('Categories'); $this->loadModel('P
public function listing($catid = null, $slug = null)
{
$this->viewBuilder()->layout('frontview');
$this->loadModel('Categories');
$this->loadModel('Products');
$categoryid=$this->Categories->find('children',['for'=>$catid]);
$catarray=array();
foreach($categoryid as $c) {
$catarray[]=$c['categoryId'];
}
debug($catarray);
$products=$this->Products->find()
->where(function ($exp, $q) {
return $exp->in('categories_id',$catarray); //line 81
});
$this->set('products',$products);
}
$catarray的调试正在进行中
[
(int) 0 => (int) 6,
(int) 1 => (int) 7,
(int) 2 => (int) 8,
(int) 3 => (int) 9,
(int) 4 => (int) 10,
(int) 5 => (int) 11,
(int) 6 => (int) 12
]
错误:注意(8):未定义变量:catarray[APP/Controller\CategoriesController.php,第81行]错误:无法生成字段(类别id)值为空的条件
function ($exp, $q) uses(catarray)
但您也可以通过以下方式简化代码:
$categoryid=$this->Categories->find('children',['for'=>$catid]);
$catarray = $categoryid->extract('categoryId');
$products=$this->Products->find()
->where(['category_id IN' => $catarray]);
该通知是由于未将其传递给匿名函数而引起的。看见