0

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

Jean Val Jean
  • 13
  • 1
  • 7
  • You should be using https://github.com/FriendsOfCake/search which solves this cleanly. – mark Aug 04 '21 at 10:44
  • dear Mark, friendsofcake search i checked and not able to find how to combine 2 search in a form, 2 tables combine in a search form – Jean Val Jean Aug 06 '21 at 09:51

0 Answers0