1

I have the following SQL query:

SELECT * FROM pm_management.system_tasks 
  WHERE eq_model = 'D9 L' and pm_type = 'H' and 
     ((equipment = '5164' and main_systems_id = 1) or 
      (equipment = 'TODOS' and main_systems_id <> 1));

I want write that query in a Cakephp find() method. I tried the following but the result was not the expected.

$sys_content = $this->SystemTask->find('all', array(
     'conditions' => array('AND' => array(
           'eq_model' => 'D9 L',
           'pm_type' => 'H',
              'OR' => array(
                  'AND' => array(
                     'equipment' => '5164',
                     'main_systems_id' => '1'
                   ),
                  'AND' => array(
                     'equipment' => 'TODOS',
                     'main_systems_id' => '<> 1'
                   )
                )
           ))
        ));

Thanks in advance for your help.

2 Answers 2

1

Use next condition. When you write conditions be aware that you cant have same key twice inside single hash(=array). So Second AND was overwrite your condition. As AND is default you can remove it like i did in code below,

$sys_content = $this->SystemTask->find('all', array(
 'conditions' => array(
       'eq_model' => 'D9 L',
       'pm_type' => 'H',
          'OR' => array(
              array(
                 'equipment' => '5164',
                 'main_systems_id' => '1'
               ),
              array(
                 'equipment' => 'TODOS',
                 'main_systems_id' => '<> 1'
               )
            )
       )
    ));

or wrap block with ['AND' => ... ] with additional array().

$sys_content = $this->SystemTask->find('all', array(
 'conditions' => array('AND' => array(
       'eq_model' => 'D9 L',
       'pm_type' => 'H',
          'OR' => array(
              array(
                'AND' => array(
                   'equipment' => '5164',
                   'main_systems_id' => '1'
                )),
              array(
                'AND' => array(
                   'equipment' => 'TODOS',
                   'main_systems_id' => '<> 1'
               ))
            )
       ))
    ));
Sign up to request clarification or add additional context in comments.

Comments

0

actually you can use $this->Model->query($yourQuery) for that purpose. Even if it is not recommended. But its the best without headache to convert it to CakePHP find.

1 Comment

Thanks for your answer Nizam. I thought that but I had a problem with this way, strangely the query was ok on sql script console but in cake doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.