LoginSignup
1
4

More than 5 years have passed since last update.

CakePHP3 データベースの基本的な操作まとめ レコードを追加する・検索する

Last updated at Posted at 2017-08-17

構築する前に

DBに、name,title,contentのフィールドが用意されているとする

レコードを追加する

src/Template/Boards/index.ctp
<h1>Database サンプル</h1>

<?=$this->Form->create($entity,['url'=>['action'=>'addRecord']])?>
<fieldset>
    <?=$this->Form->text("name")?>
    <?=$this->Form->text("title")?>
    <?=$this->Form->textarea("content")?>
</fieldset>
<?=$this->Form->button("送信")?>
<?=$this->Form->end()?>

<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>NAME</th>
            <th>TITLE</th>
            <th>CONTENT</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach($data as $obj): ?>
        <tr>
            <td><?=$obj->id ?></td>
            <td><?=h($obj->name) ?></td>
            <td><?=h($obj->title) ?></td>
            <td><?=h($obj->content) ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
/src/Controller/BoardsController.php
<?php 
    namespace App\Controller;
    class BoardsController extends AppController {
        public function index() {
            $data = $this->Boards->find('all');
            $this->set('data',$data);
            $this->set('entity',$this->Boards->newEntity());
        }

        public function addRecord() {
            if($this->request->is('post')) {
                //エンティティクラスのインスタンスを作成。
                //newEntityは引数に指定されたデータを元にインスタンスを作成する
                //$this->request->dataはPOSTされたデータがまとめられているので、これを引数にする
                $board = $this->Boards->newEntity($this->request->data);
                //saveメソッドで作成したBoardインスタンスを保存し、データベーステーブルにレコードとして保存
                //newEntityはオブジェクトを作成しているだけなので、DBに何も影響しないので注意
                $this->Boards->save($board);
            }
            return $this->redirect(['action'=>'index']);
        }
    }

IDでレコードを検索する画面を作成する

src/Template/Boards/index.ctp
<h1>Database サンプル</h1>

<?=$this->Form->create($entity,['url'=>['action'=>'index']])?>
<fieldset>
    <?=$this->Form->text("id")?>
</fieldset>
<?=$this->Form->button("送信")?>
<?=$this->Form->end()?>

<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>NAME</th>
            <th>TITLE</th>
            <th>CONTENT</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach($data as $obj): ?>
        <tr>
            <td><?=$obj->id ?></td>
            <td><?=h($obj->name) ?></td>
            <td><?=h($obj->title) ?></td>
            <td><?=h($obj->content) ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
<?php 
    namespace App\Controller;
    class BoardsController extends AppController {
        public function index(){
            $this->set('entity',$this->Boards->newEntity());
            if ($this->request->is('post')) {
             //findメソッドの第二引数に連想配列で検索に関する設定情報をまとめる
                $data = $this->Boards->find('all', [
                    'conditions'=>['id' => $this->request->data['id']]
                ]);
            } else {
                $data = $this->Boards->find('all');
            }
            $this->set('data',$data);
        }
      }

nameのあいまい検索をする

src/Template/Boards/index.ctp
<h1>Database サンプル</h1>

<?=$this->Form->create($entity,['url'=>['action'=>'index']])?>
<fieldset>
    <?=$this->Form->text("name")?>
</fieldset>
<?=$this->Form->button("送信")?>
<?=$this->Form->end()?>

<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>NAME</th>
            <th>TITLE</th>
            <th>CONTENT</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach($data as $obj): ?>
        <tr>
            <td><?=$obj->id ?></td>
            <td><?=h($obj->name) ?></td>
            <td><?=h($obj->title) ?></td>
            <td><?=h($obj->content) ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
/src/Controller/BoardsController.php
<?php 
    namespace App\Controller;
    class BoardsController extends AppController {
        public function index(){
            $this->set('entity',$this->Boards->newEntity());
            if ($this->request->is('post')) {
                //findメソッドの第二引数に連想配列で検索に関する設定情報をまとめる
                $data = $this->Boards->find('all', [
                    'conditions'=>['name like' => "%{$this->request->data['name']}%"]
                ]);
            } else {
                $data = $this->Boards->find('all');
            }
            $this->set('data',$data);
        }

findの基本形

find('all',連想配列);

第一引数

第一引数 内容
'all' 全レコードを得る
'list' レコードのリストを得る
'threaded' スレッド状のレコードを得る

第二引数

オプションを設定するために用意するもの。
なければ、記述しなくて良い。
連想配列で記述する

//idのフィールドの値がdata['id']の等しいものだけ検索する
 $data = $this->Boards->find('all', [
                    'conditions'=>['id' => $this->request->data['id']]
                ]);
第二引数 内容
'conditions' 条件の設定。条件をつけるフィールド名をキーにした連想配列として用意する
'fields' 取得するフィールド名を配列として用意する
'recursive' 再帰的に取得する深度
'order' 取得順。順序を示す数字または名前を配列として用意する
'limit' 取得するレコード数
'offset' 取得開始位置のオフセット
'page' 取得するページ数
1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4