3.5.3.4.4 postConditions
postConditions(array $data, mixed $op, string $bool, boolean $exclusive)
postConditions(array $data, mixed $op, string $bool, boolean $exclusive)
このメソッドを使用すると、POST された一連のモデルのデータ(Htmlヘルパーと互換のある入力値)をモデル用の find 条件に変換できます。この関数は、検索ロジックを素早く構築するためのショートカットです。たとえば、管理権限のあるユーザが、どの項目が入力されたかを知るために順序を検索したい。CakePHP の Form ヘルパーや Html ヘルパーを使用して、Order モデルに基づいて素早くフォームを生成することができます。コントローラのアクションは、そのフォームから POST されたデータを使用して find 条件を組み立てることができます。:
function index() {
$o = $this->Orders->findAll($this->postConditions($this->data));
$this->set('orders', $o);
}
function index() {$o = $this->Orders->findAll($this->postConditions($this->data));$this->set('orders', $o);}
仮に $this->data[‘Order’][‘destination’] の値が “Old Towne Bakery” と等しい場合、postConditions はその条件を配列に変換し、Model->findAll() メソッドで使用できるようにします。この場合、array(“Order.destination” => “Old Towne Bakery”) のようになります。
もし用語内で別の SQL オペレータを使用したい場合、第2引数を使用します。
/*
$this->data の内容
array(
'Order' => array(
'num_items' => '4',
'referrer' => 'Ye Olde'
)
)
*/
// 最低4つの項目があり、 ‘Ye Olde’ を含む orders を取得しましょう
$o = $this->Order->findAll($this->postConditions(
$this->data,
array('>=', 'LIKE')
));
/*$this->data の内容array('Order' => array('num_items' => '4','referrer' => 'Ye Olde'))*/// 最低4つの項目があり、 ‘Ye Olde’ を含む orders を取得しましょう$o = $this->Order->findAll($this->postConditions($this->data,array('>=', 'LIKE')));
オペレータを指定したキーの順番は、$this->data 配列内のカラムの順です。num_items は1番目なので、>= オペレータが適用されます。
3番目の引数は、find 条件内でどの SQL 真偽値オペレータを使用するかをCakePHP に知らせます。‘AND’, ‘OR’, ‘XOR’ のような文字列はすべて有効な値です。
最後に、最後の引数に true をセットすると、$op パラメータは配列となり、$op に含まれないフィールドは返される条件に含まれないでしょう。
