ひとり勉強ログ

ITエンジニアの勉強したことメモ

#11 データベース検索 -select, order-

今回は検索を補足する機能として select, order を実装する。

select - 取り出すフィールドを指定

テーブルにあるフィールドから、必要な項目だけを指定する。 [sql] ->select([ field1, field2, field3,]) [/sql]

order - 並び順を指定

検索結果を特定の順番で並び替える。 [sql] ->order([ field => 'Asc' ]) [/sql] 並べ替えの指定には「Asc」(昇順)又は「Desc」(降順)を指定する。

「id」「name」の値だけを取り出し、「name」の順に並べる

「HeroesController」に記述していてた「find」アクションを変更する。

■場所 \htdocs\chococake\src\Controller ■ファイル名 HeroesController.php

[php highlight="53-67"] <?php namespace App\Controller;

use App\Controller\AppController;

/* * / class HeroesController extends AppController { public function add() { if ($this->request->is('post')) { $hero = $this->Heroes->newEntity(); $hero = $this->Heroes->patchEntity($hero, $this->request->data);

  if ($this-&gt;Heroes-&gt;save($hero)) {
    return $this-&gt;redirect(['action' =&gt; 'index']);
  }
}

}

public function index() { $this->set('heroes', $this->Heroes->find('all')); }

public function edit($id = null) { $hero = $this->Heroes->get($id); if ($this->request->is(['post', 'put'])) { $hero = $this->Heroes->patchEntity($hero, $this->request->data); if ($this->Heroes->save($hero)) { return $this->redirect(['action' => 'index']); } } else { $this->set('hero', $hero); } }

public function delete($id = null) { $hero = $this->Heroes->get($id); if ($this->request->is(['post', 'put'])) { if ($this->Heroes->delete($hero)) { return $this->redirect(['action' => 'index']); } } else { $this->set('hero', $hero); } }

public function find() { // $heroes = ; if ($this->request->is('post')) { $find = $this->request->data['find']; $heroes = $this->Heroes->find() // 今回変更部分:findアクション ->select(['id', 'name']) ->order(['name' => 'Asc']) ->where(['name like ' => '%' . $find . '%']); } else { $heroes = ; } $this->set('heroes', $heroes); }

} [/php]

解説

[php] ->select(['id', 'name']) [/php] select メソッドで「id」「name」のフィールドを選択する。

[php] ->order(['name' => 'Asc']) [/php] order メソッドで「name」フィールドの昇順で全体を並べかえる。

[php] } else { $heroes = []; } [/php]

ビューテンプレートファイルは前回のまま変更なし。

以下URLにアクセスし、「検索」ボタンを押下して「id」と「名前」が「名前」の昇順で表示されればOK。
http://localhost/chococake/heroes/find

「power」が100以上の全項目の値を取り出し、「power」の降順に並べる

「HeroesController」に記述していてた「find」アクションを変更する。

■場所 \htdocs\chococake\src\Controller ■ファイル名 HeroesController.php

[php highlight="53-70"] <?php namespace App\Controller;

use App\Controller\AppController;

/* * / class HeroesController extends AppController { public function add() { if ($this->request->is('post')) { $hero = $this->Heroes->newEntity(); $hero = $this->Heroes->patchEntity($hero, $this->request->data);

  if ($this-&gt;Heroes-&gt;save($hero)) {
    return $this-&gt;redirect(['action' =&gt; 'index']);
  }
}

}

public function index() { $this->set('heroes', $this->Heroes->find('all')); }

public function edit($id = null) { $hero = $this->Heroes->get($id); if ($this->request->is(['post', 'put'])) { $hero = $this->Heroes->patchEntity($hero, $this->request->data); if ($this->Heroes->save($hero)) { return $this->redirect(['action' => 'index']); } } else { $this->set('hero', $hero); } }

public function delete($id = null) { $hero = $this->Heroes->get($id); if ($this->request->is(['post', 'put'])) { if ($this->Heroes->delete($hero)) { return $this->redirect(['action' => 'index']); } } else { $this->set('hero', $hero); } }

public function find() { // $heroes = ; if ($this->request->is('post')) { $find = $this->request->data['find']; $heroes = $this->Heroes->find() // 今回変更部分:findアクション // ->select(['id', 'name']) ->where(function ($exp) { return $exp->gte('power' , 100); }) ->order(['power' => 'Desc']) ->where(['name like ' => '%' . $find . '%']); } else { $heroes = ; } $this->set('heroes', $heroes); }

} [/php]

解説

[php] // ->select(['id', 'name']) [/php] 全項目表示させるため、コメントアウト

[php] ->where(function ($exp) { return $exp->gte('power' , 100); }) [/php] 「power」が「100」以上を抽出。

[php] ->order(['power' => 'Desc']) [/php] order メソッドで「power」フィールドの降順で全体を並べかえる。

以下URLにアクセスし、「検索」ボタンを押下して「超人強度」が「100」以上の全項目が降順で表示されればOK。
http://localhost/chococake/heroes/find