$変数 = 《Query》->newExpr();
$fnc = function($exp, $find) {
return $exp->gte('age', $find * 1);
};この関数は、QueryExpressionを$expに、そして検索テキストを$findにそれぞれ渡して呼び出すようにしてあります。この中で、QueryExpressionのgteというメソッドを使って検索条件を設定してあります(このメソッドについては後述)。$query = $this->Persons->find();これで一通りの準備が整いました。実際の検索はwhereを使って行いますが、この引数に先ほどの無名関数$fncを指定してやればいいのです。
$exp = $query->newExpr();
$persons = $query->where($fnc($exp,$find));これで、$fnc関数の結果がwhereに引数として指定され、その結果、ageの値が$find以上のものだけが$personsに返される、というわけです。
※リストが表示されない場合
AddBlockなどの広告ブロックツールがONになっているとリストなどが表示されない場合があります。これらのツールをOFFにしてみてください。
public function find() {
$persons = [];
if ($this->request->is('post')) {
$find = $this->request->data['find'];
$query = $this->Persons->find();
$exp = $query->newExpr();
$fnc = function($exp, $find) {
return $exp->gte('age', $find * 1);
};
$persons = $query->where($fnc($exp,$find));
}
$this->set('persons', $persons);
$this->set('msg', null);
}
| << 前へ | 次へ >> |