首页 > 解决方案 > 按不同表格过滤产品 - Cakephp 3.8

问题描述

朋友们,如果有人可以帮助我,我将不胜感激。

输入关键字后,我创建了一个字段来按品牌、州和城市过滤产品。当我有搜索输入并且品牌字段运行良好时。但是,当我添加更多字段时,例如州和城市,他不会搜索。

ProductsTable.php

.
$this->belongsTo('Users', [
       'foreignKey' => 'user',
       'joinType' => 'INNER',
]);
.

用户表.php

$this->hasMany('Products', [
       'foreignKey' => 'user_id',
]);

最后,我做这样的过滤方法:

public function search()
    {           
        $this->viewBuilder()->setLayout('search');

        $product = $this->paginate = [
            'contain' => ['Brands', 'Users'],
            'conditions' => ['and' => [
                'Products.name like' => '%'. $this->request->getQuery('search') . '%',
                'Brands.id like' => '%'.$this->request->getQuery('brand').'%',
                'Users.state like' => '%'.$this->request->getQuery('estate').'%',
                'Users.city like' => '%'.$this->request->getQuery('city').'%',
            ]
        ],
            'order' => ['Products.price' => 'ASC' ]
        ];
        
        $this->set('products', $this->paginate($this->Products));
        $this->set('_serialize', ['products', 'brands', 'users']);

    }

该方法不会给出错误,但它会像我之前所说的那样进行过滤。

如果有人可以分析,我将不胜感激。

标签: phpcakephpcakephp-3.0

解决方案


推荐阅读