Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
想在cakephp 3中获得子类别产品吗_Php_Mysql_Cakephp - Fatal编程技术网

想在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]);

该通知是由于未将其传递给匿名函数而引起的。看见