0

私はこのような長いクエリを持っています:

$a=$this->Signin->find('all',
    array(
        'fields'=> array(
            .....
        ),
        'conditions' => array('mytable.user_type ...),
        'order' => ....
        'limit' => ....
));

user_type列は0または1の値を取ることができます。関数パラメーターが$typeあります。このパラメーターに関して、SQLクエリを変更する必要があります。について$typeお問い合わせします

  • "mytable.user_type" = 1、
  • "mytable.user_type" = 0、
  • "mytable.user_type"=1または"mytable.user_type= 0

同じクエリを3回書きたくありません。条件部分のみ変更したい。出来ますか?

4

2 に答える 2

2

$ typeを配列として定義し、$typeの値を管理できます

複数の値のように

$type = array(1,0);
'conditions' => array('mytable.user_type'=>$type)

単一値の場合

$type = array(1);
'conditions' => array('mytable.user_type'=>$type)

したがって、条件に従って$typeを操作する必要があります。この作業がうまくいくことを願っています。

于 2012-08-27T18:18:41.583 に答える
1

これを試して:

$user_type = ($type == 'something') ? 1 : 0 ;

次に、あなたの条件で:

'conditions' => array('mytable.user_type' => $user_type);

user_typeが取ることができる値は2つだけなので、3番目のクエリ("mytable.user_type"=1 OR "mytable.user_type=0)は不要です

編集 :

if($type == 'conditions_where_usertype_is_necessary') $user_type = ($type == 'something') ? 1 : 0 ;

次に、あなたの条件のために:

if( isset($user_type) ) {
    $options['conditions']['mytable.user_type'] = $user_type;
}
$options['conditions']['other_conditions'] = ...;
$this->Signin->find('all', $options);
于 2012-08-27T19:20:08.783 に答える