cakephp4で値を取得する、DBに登録するやり方のメモ

こんにちは!
dynaです!
この記事は、 cakephpでview画面に値を表示したり、値をとってくるときのメモです。
久しぶりにcakephpを触ったらバージョン4になってました。
書き方忘れちゃってたので、自分のための備忘録です(;’∀’)
同じような方がいればご参考までに( ^ω^)・・・
- Xamppを使い、ローカル環境で作成
- cakephp4を使用
- phpは7.2以上
- 導入部分は割愛しています
- MVCについては割愛しています
- 値を表示する、とってくるという基本だけを記録してます
全体のサンプル
cakephp4 + Bootstrap の無料テーマで、簡単に表示する部分を作成しました。
気が向いたらもうちょいデザインを直そうかな(;’∀’)
スクショしたのはこんな感じです。(view画面)

controllerからviewに値を渡す
controllerからviewに値を渡すときは、 $this->set(compact());を使って渡ります。
ここでは、
$favorite = ‘Mrs.Greenapple’;
$name = ‘dyna’;
をビュー画面に渡します。
1 2 3 4 5 6 7 8 9 10 11 12 13 | class ○○(任意の名前)Controller extends AppController { public function index() { //controllerからの出力 $favorite = 'Mrs.Greenapple'; $name = 'dyna'; //viewへのセット $this->set(compact('favorite', 'name')); } } |
1 2 3 4 5 | <h3>controllerから値を渡す</h3> <p class="text-info">好きなもの:<?= $favorite ?></p> <p class="text-info">名前:<?= $name ?></p> |
出力結果↓

$this->set(compact[ ‘ 変数名1 ‘ , ‘ 変数名2 ‘ ]);とすると、compact関数が名前と同じ変数名を探してくれます。
compact関数については以下。
各引数について、compact() は現在のシンボルテーブルにおいてその名前を有する変数を探し、 変数名がキー、変数の値がそのキーに関する値となるように追加します。
引用元:https://www.php.net/manual/ja/function.compact.php
DBからの値を表示
次はDBから値をとってきて、viewに表示させたいと思います。
DBは作成しておきます。
「test_db」としてサンプルを作成しておきます。
※DBの連携部分は割愛します。


このtestsのDBから下記の値をとってきます。

とりあえず全部出力してみる。
1 2 3 | use Cake\ORM\TableRegistry; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class ○○Controller extends AppController { public function initialize(): void { parent::initialize(); //ここではtestsのDBを指定 $this->tests = TableRegistry::get('Tests'); } public function index() { //DBからの取得 $query = $this->tests->find(); $data = $query->all(); $this->set(compact('data')); } } |
1 2 3 4 5 6 7 | <?php foreach ($data as $row): ?> <p><?= $row->test_id ?></p> <p><?= $row->name ?></p> <p><?= $row->comment ?></p> <?php endforeach; ?> |
出力結果↓(HTMLはちょっと変えて作成しています)

DBから特定の値を取得
次は、DBの1部だけ取ってきたい、というときの記載の仕方。
ここでは、 $testName = $query->where([‘name’ => ‘aaa’]); で
testsのDBの中から、「name」の項目が「aaa」を探してくるように指定しています。

先ほどの public function index(){ の最後の方に以下を追加、 $this->set(compact を修正。
結果を $testNameに格納して、ビューに値を渡します。
1 2 3 4 5 6 | //DBから特定のものを表示させる $testName = $query->where(['name' => 'aaa']); $this->set(compact('data','testName')); |
コメントと名前を表示させます。
1 2 3 4 5 6 | <?php foreach ($testName as $obj): ?> <p><?= $obj->name ?></p> <p><?= $obj->comment ?></p> <?php endforeach; ?> |
出力結果↓

フォームから値をとってきてDBに登録
まずはフォームから値をとってくるやり方。
こんなフォームがあったとします。

名前とコメントを入力して、登録ボタンを押したらDBに登録します。
フォームから値をとるには $this ->request ->getDataを使う。
1 2 3 4 5 | //フォームから値を取得 $getName = $this ->request ->getData('nametext'); $getComment = $this ->request ->getData('comment'); |
1 2 3 4 5 6 7 8 9 10 11 | <?=$this->Form->create(null,['type' => 'post']);?> <span class="input-group-text"> <i class="material-icons">名前</i> </span> //名前の入力フォーム <?= $this->form->text('',['name'=>'nametext','class'=>'form-control','placeholder'=>'First Name'])?> //コメントの入力フォーム <?= $this->form->textarea('',['name'=>'comment','class'=>'form-control'])?> <?=$this->Form->end()?> |
ボタンが押下されたかを取得。
1 2 3 4 | //ボタン押下を取得 $button = $this->request->getData('btnT'); |
1 2 3 | <?= $this->Form->button('登録', ['action' => 'index','name'=>'btnT','class'=>'btn-primary'])?> |
ボタンの値が「Nullでない」かつ、「フォームに入力値がある」ならDBに登録します。
※↑この条件は if文 で作成しました。
DBへの登録の仕方は下記。
1 2 3 4 5 6 7 | //登録 $entity = $this->tests->newEmptyEntity(); $entity->name = $getName; $entity->comment = $getComment; $this->tests->save($entity); |
ちょいちょいcakephp3と書き方が変わってるんだね。
試しに入力してみます。

ちゃんと7番で登録されました。

書き方変わっていたり、忘れてしまっていたので備忘録です。
もし「ここ違ってるよ~等」あれば、お問い合わせからお願いします!