cake php where 条件返回所有值

cake php where condition returning all value

我正在使用cake php 3. 我想使用where 条件获取数据。但它显示了来自 table 的所有数据,而没有使用 where 条件过滤数据。 这是我对插入条件数据

的看法
 <?= $this->Form->create() ?>
    <?php
        echo $this->Form->control('category_id', ['options' => $categories,'empty'=>'Choose']);
    ?>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

这是我的控制器

if(!empty($this->request->data['category_id'])){
        $categoryId = $this->request->data();
        $searcehProduct = $this->Products->searchProductsByCategory($categoryId);
        dd($searcehProduct);
    }

这是我的 ProductTables.php 条件代码。

 public function searchProductsByCategory($id){
    $products = TableRegistry::get('Products');
    $query = $products->find('all', [
        'where' => ['category_id' => $id]
    ]);
    return $query;
}

TIA

你的错误只是你将 'conditions' 替换为 'where'

在你的控制器中使用

$categoryId = $this->request->getData('category_id');

并在您的table中使用

'conditions' => ['category_id' => $id]

它应该有效

无论如何,这不是创建自定义查找器的方式。但您甚至不必创建自定义查找器,只需:

使用一个Dynamic Finder

其实你可以做到

if($this->request->getData('category_id'))
{
    $categoryId = $this->request->getData('category_id');
    $searcehProduct = $this->Products->findByCategoryId($categoryId);
    dd($searcehProduct);
}

不需要改变你的Table,这是一个动态构造的查找器