简体   繁体   中英

How to use condition inside another condition in cakephp

I have one field answer. Also I have two ids ie men and women id fields name as ca_field_id in another table.

My requirement is I want to get data with following conditions

  1. with both ids men(id=37) and women(id=38)
  2. age of men 67
  3. age of women 65

But I am getting data of both ie men with 67 age as well as women with 67 age and vice versa.

With below query:

$numberofemployeesmen = 37;
$numberofemployeeswomen =38;

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

Also I tried as following but did not work

$numberofemployeesmen = 37;
$numberofemployeeswomen =38;

$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' =>$numberofemployeesmen),
            65 AND array('CaAnswer.ca_field_id'=>$numberofemployeeswomen))
        )
    )
 );

Please tell how I can solve this...

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).

Have you tried

$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))
        )
    )
 );

If I am not mistaken array($numberofemployeesmen,$numberofemployeeswomen) creates an array like this 0=>$numberofemployeesmen 1=>$numberofemployeeswomen

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM