参考にさせて頂いたページ
クエリー、マジック検索、AND/OR検索(2/3):初心者のためのCakePHP2.0 プログラミング入門
ホストマシン環境
ゲストマシン環境
- PHP 5.3.3
- CentOS release 6.6 (Final)
- cakephp2.5.6
- mysql Ver 14.14 Distrib 5.6.21, for Linux (x86_64) using EditLine wrapper
- Apache/2.2.15 (Unix)
NumTenSamplesControllerコントローラーにandorアクションを追加
参考ページにならって以下の通りですが、
MySampleDataではなく、NumTenSampleモデルを使用しているので、そこは置換しています。
public function andor() {
// レイアウト関係
$this->layout = "Sample";
$this->set("header_for_layout", "Sample Application");
$this->set("footer_for_layout",
"copyright by SYODA-Tuyano. 2011.");
// post時の処理
if ($this->request->is('post')) {
$name = $this->request->data('NumTenSample.name');//postされた文字列
$mail = $this->request->data('NumTenSample.mail');//postされた文字列
//テーブルから検索する為の配列を設定
$opt = array("OR" => array (
"NumTenSample.name" => $name,
"NumTenSample.mail" => $mail
)
);
//上で設定した配列$optを引数にして、テーブルからデータを取得
//findallを使用している
$data = $this->NumTenSample->
find('all',array('conditions' => $opt));
//テーブルから取得した$dataをset
$this->set('data',$data);
}
}
andor.ctpの作成
参考ページにならって以下の通りですが、 echo $this->Form->create('NumTenSample');にしています。
<h1>Index Page</h1>
<p>NumTenSample Find View.</p>
<?php
echo $this->Form->create('NumTenSample');
echo $this->Form->input('name');
echo $this->Form->input('mail');
echo $this->Form->end('Submit');
?>
<?php if (isset($data)): ?>
<pre><?php print_r($data); ?></pre>
<?php endif; ?>
findメソッド
//オプションを設定
$opt = array("OR" => array (
"NumTenSample.name" => $name,
"NumTenSample.mail" => $mail
)
);
//$data = $this->モデルクラス名->find('all',array('conditions' => オプション));
$data = $this->NumTenSample->find('all',array('conditions' => $opt));