如何在cakephp的另一个条件中使用条件

时间:2017-04-13 10:41:54

标签: php mysql arrays cakephp

我有一个现场答案。我还有两个id,即男性和女性id字段在另一个表中名称为try{ InputStream in = this.getClass().getResourceAsStream("/suggest.properties"); Properties props = new Properties(); props.load(in); in.close(); URL url = this.getClass().getResource("/suggest.properties"); File fileObject = new File(url.toURI()); FileOutputStream out = new FileOutputStream(fileObject); props.setProperty("country", "america"); props.store(out, null); out.close(); }catch(Exception e) { System.out.println(e.getMessage()); }

我的要求是我希望获得具有以下条件的数据

  1. 同时具有男性(id = 37)和女性(id = 38)
  2. 男性年龄67
  3. 女性年龄65
  4. 但是我得到的数据包括67岁的男性和67岁的女性,反之亦然。

    使用以下查询:

    ca_field_id

    我也试过以下但没有工作

    $numberofemployeesmen = 37;
    $numberofemployeeswomen =38;
    
    $caanswer = $this->CaAnswer->find('all', 
        array(
            'conditions' => array(
                'CaAnswer.ca_field_id' => array($numberofemployeesmen,$numberofemployeeswomen),
                'CaAnswer.answer' => array(67,65)
             )
         )
     );
    

    请告诉我如何解决这个问题......

2 个答案:

答案 0 :(得分:1)

Try this:

$numberofemployeesmen = 37;
$numberofemployeeswomen = 38;

$caanswer = $this->CaAnswer->find('all', 
    [
        'conditions' => [
            'OR' => [
                [
                    'CaAnswer.ca_field_id' => $numberofemployeesmen,
                    'CaAnswer.answer' => 67
                ],
                [
                    'CaAnswer.ca_field_id' => $numberofemployeeswomen,
                    'CaAnswer.answer' => 65
                ],
             ]
         ]
     ]
 );

If that doesn't work, you might need to add additional AND keys within the OR:

$caanswer = $this->CaAnswer->find('all', 
    [
        'conditions' => [
            'OR' => [
                [
                    'AND' => [
                        'CaAnswer.ca_field_id' => $numberofemployeesmen,
                        'CaAnswer.answer' => 67
                    ],
                ],
                [
                    'AND' => [
                        'CaAnswer.ca_field_id' => $numberofemployeeswomen,
                        'CaAnswer.answer' => 65
                    ],
                ],
             ]
         ]
     ]
 );

Notice in this one, the AND are within an array of their own so the second AND key doesn't overwrite the first.

The idea is that you're saying I want either this first set of AND conditions OR the second set of AND conditions. But within each, both conditions need met.

It might not be exactly right, but these should give you the idea of how it can be done. (Just going off the top of my head).

答案 1 :(得分:0)

你试过吗

$caanswer = $this->CaAnswer->find('all', 
    array('conditions' => array(
        'CaAnswer.ca_field_id' => array($numberofemployeesmen,$numberofemployeeswomen),
        'CaAnswer.answer'=> array(
            67 AND array('CaAnswer.ca_field_id' =>0),
            65 AND array('CaAnswer.ca_field_id'=>1))
        )
    )
 );

如果我没有弄错阵列($ numberofemployeesmen,$ numberofemployeeswomen)创建一个这样的数组0 => $ numberofemployeesmen 1 => $ numberofemployeeswomen