Cakephp 3 Search Function

104 Views Asked by At

got a question about cakephp3 search function, is it possible to search among two tables/Controllers in a search form?

this is the codes, am trying to search from categories name and also products name, their relationship is products has category id

this is the code from the Controller

        $c = $this->request->getQuery('c');

        if(!empty($c)) {
        $this->loadModel('Categories');
        
       
        $category = TableRegistry::get('Categories');

        $query = $this->Products->find()->contain(['Categories'=>['Products']])
        ->where(['Categories.name LIKE' =>  '%' .   $c. '%']);
       
        }
        $this->set(compact('c','query'));



        $key = $this->request->getQuery('key');

        if(!empty($key)) {
       
        $products->where(['Products.name LIKE ' => '%'.$key.'%']);

        }
    
        $this->set(compact('key'));

this is from the HTML side, where i name="c", it only search for c and not key,

      <?php echo $this->Form->create(null, ['type' => 'get', 'valueSources' => 
      'query'); ?>

      <?php echo $this->Form->control('search', ['type' => 'hidden']); ?>

      <input type="text" class="form-control" name="c" placeholder="Search...">

      <span class="input-group-append">
        <button type="submit">

      <?php echo $this->Form->end(); ?>

is there a method to combine both search in a form?

thank you for your helps

0

There are 0 best solutions below