前提
開発環境
docker,cakephp4,mysql
を使用しています。
作成したテーブルは
companies,projects,project_users(中間テーブル),documents,users
です。
cakephp4でファイル検索機能を主にしたシステム開発を行っています。
状態としてはDBを作成し、template、model、controllerのbakeを行ったばかりです。
また、検索バーの作成を行いました。
現在の問題点としては、
記述式の検索バーを作ることはできたのですが、選択式の検索が実装できません。
実現したいこと
searchと表の間にセレクトボックスをいれてプロジェクト番号を選択することで選択したプロジェクト番号を持つカラムのみの表示にしたいです。
2つとも値がセットされていたらAND検索にしたいです。
該当のソースコード
DocumentsController.php
cakephp4
1class DocumentsController extends AppController 2{ 3 public function index() 4 { 5 $key = $this->request->getQuery('key'); 6 7 if($key){ 8 9 $query = $this->Documents->find('all')->where(['name like' =>'%'.$key.'%']); 10 11 }else{ 12 13 $query = $this->Documents; 14 15 } 16 17 $this->loadModel('Projects'); 18 $projects = $this->Projects->find('all'); 19 $this->set(compact('projects')); 20 21 22 23 $this->paginate = [ 24 'contain' => ['Projects', 'Users'], 25 ]; 26 $documents = $this->paginate($query); 27 28 $this->set(compact('documents')); 29 30 } 31
<div class="documents index content"> <?= $this->Html->link(__('New Document'), ['action' => 'add'], ['class' => 'button float-right']) ?> <h3><?= __('Documents') ?></h3> <?= $this->Form->create(null, ['type' => 'get']) ?> <?= $this->Form->control('key', ['label' => 'Search', 'value' => $this->request->getQuery('key')]) ?> <?= $this->Form->end() ?> <div class="table-responsive"> <table> <thead> <tr> <th><?= $this->Paginator->sort('プロジェクト番号') ?></th> <th><?= $this->Paginator->sort('期') ?></th> <th><?= $this->Paginator->sort('枝番') ?></th> <th><?= $this->Paginator->sort('ファイル区分') ?></th> <th><?= $this->Paginator->sort('担当者') ?></th> <th><?= $this->Paginator->sort('created') ?></th> <th><?= $this->Paginator->sort('modified') ?></th> <th class="actions"><?= __('Actions') ?></th> </tr> </thead> <tbody> <?php foreach ($documents as $document): ?> <tr> <td><?= $document->has('project') ? $this->Html->link($document->project->recordnumber, ['controller' => 'Projects', 'action' => 'view', $document->project->id]) : '' ?></td> <td><?= $document->has('project') ? $this->Html->link($document->project->period, ['controller' => 'Projects', 'action' => 'view', $document->project->id]) : '' ?></td> <td><?= h($document->edaban) ?></td> <td><?= $this->Number->format($document->documentfile) ?></td> <td><?= $document->has('user') ? $this->Html->link($document->user->name, ['controller' => 'Users', 'action' => 'view', $document->user->id]) : '' ?></td> <td><?= h($document->created) ?></td> <td><?= h($document->modified) ?></td> <td class="actions"> <?= $this->Html->link(__('View'), ['action' => 'view', $document->id]) ?> <?= $this->Html->link(__('Edit'), ['action' => 'edit', $document->id]) ?> <?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $document->id], ['confirm' => __('Are you sure you want to delete # {0}?', $document->id)]) ?> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> <div class="paginator"> <ul class="pagination"> <?= $this->Paginator->first('<< ' . __('first')) ?> <?= $this->Paginator->prev('< ' . __('previous')) ?> <?= $this->Paginator->numbers() ?> <?= $this->Paginator->next(__('next') . ' >') ?> <?= $this->Paginator->last(__('last') . ' >>') ?> </ul> <p><?= $this->Paginator->counter(__('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')) ?></p> </div> </div>
試したこと
この方のリンクを参考にしました。
https://teratail.com/questions/78668
補足情報
cakephp4を触り初めてまだ1ヶ月たっていない初心者です。
基礎的な質問かもしれませんがご回答のほどよろしくお願いいたします。
あなたの回答
tips
プレビュー