0

Friends, I am displaying a user's profile along with the products registered by him. My relationships are correct. I can list all products as normal, along with user information. But my product search field in the user's view doesn't work.

This is my code detail_user.ctp

//user information only above. (Address, Telephone, etc.)
.
.
.
  <h2 class="bd-title mt-2" id="content">Products Gallery</h2>
    <div class="card mt-2 mb-2">
      <div class="card-header">
        <?= $this->Form->create('', ['type' => 'get']); ?>
        <div class="input-group">
          <?= $this->Form->control('keyword', array('class' => 'form-control','name'=> 'search','label' => false,'required' => true,));?>
          
          <div class="input-group-append">
            <?= $this->Form->button(__('Search')); ?>
          </div>
        </div>
      </div>
      <div class="table-responsive">
        <table class="table table-hover">
          <tbody>
            <?php foreach ($user->products as $product): ?>

              <td> 
                <p class="title mb-0"><?= h($product->name) ?> </p>
                <var class="price text-muted">R$ <?= number_format($product->price);?></var>
              </td>
              <td>  <br></td>
              <td width="130">
                <?= $this->Html->link(__('Open'), ['controller' => 'Produtos','action' => 'details_product','prefix' => false, $product->slug], array('class' => 'btn btn-primary')) ?>

              </td>
            </tr>
            <?php endforeach; ?>

          </tbody></table>
        </div> <!-- table-responsive .end// -->
      </article>

And my method is like this:

 public function detailUser($slug)
  {
  
    $keyword = $this->request->getQuery('keyword');

    $query = $this->Users
    ->find()
    ->matching('Products', function(\Cake\ORM\Query $q) {
        return $q->where(['name LIKE' => ('%'. $this->request->getQuery('keyword') . '%')]);
    });

    $usuario = $this->Users
      ->findBySlug($slug)
      ->contain(['Products', 'Groups'])
      ->firstOrFail();

    $query = $this->Users->find('all');
    $this->set('user', $user);
    $this->set('query', $this->paginate());
  }

I have difficulties to solve this method. At the moment, when I type the product name, the url changes, but the product is not searched.

I consulted the generated SQL and everything is correct, but it does not filter the word entered in the search field and also does not give an error.

I appreciate any comments!

E. Biagi
  • 452
  • 3
  • 12
  • Sounds like you're looking for **https://stackoverflow.com/questions/26799094/how-to-filter-by-conditions-for-associated-models** – ndm Aug 17 '20 at 06:54
  • I made the code change. The error does not appear, but the search is not done! The URL changes to "? Search = keyword", but the search is not done – E. Biagi Aug 17 '20 at 13:42

0 Answers0