简体   繁体   中英

CakePHP ignores condition

I have the following problem with CakePHP:

Two tables are joined (filters and accounts). Then I am building conditions and only the second condition Account.active =>1 gets executed. If I print the result, there are still showing filters that are having another mode_id than 3.

$joins= array( 
    array('table' => 'filters',
          'alias' => 'Filter',
          'type' => 'right',
          'conditions' => array(
          'Filter.account_id = Account.id',
                )
            ),
        );

Then I execute the request including joins and conditions

$activeAccounts = $this->Account->find('all',array(
 'conditions'=>array('AND'=>array('Filter.mode_id'=>3,'Account.active'=>1)),
 'joins'=>$joins));

The models were checked and no problems identified. Filter belongs to Account. Account has many Filter.

Below the query that is generated. The results are still showing filters with Filter.mode_id other than 3

Here is the query that is generated. The results are still containing rows with Filter.mode_id other than 3 despite the fact that one condition is 'Filter.mode_id'=>3

SELECT `Account`.`id`, `Account`.`user_id`, `Account`.`name`, 
`Account`.`api_key`, `Account`.`account_number`, `Account`.`remaining_balance`, 
`Account`.`investment_size`, `Account`.`active` 
FROM `baseline_db`.`accounts` AS `Account` 
right JOIN `baseline_db`.`filters` AS `Filter` 
  ON (`Filter`.`account_id` = `Account`.`id`) 
WHERE ((`Filter`.`mode_id` = 3) AND 
(`Account`.`active` = '1'))

Like say Oldskool, use the Model associations and for your condition, The "AND" is not necessary, you cant put :

$activeAccounts = $this->Account->find('all',array(
    'conditions' => array(
        'Filter.mode_id'=>3,
        'Account.active'=>1
    )
));

the request you want to make with the type of relation you have, seem to me weird. If i understand, perhaps with something like that :

$this->loadModel('Filter');
$filters =$this->Filter->find("list", array(
    'conditions' => array('Filter.mode_id' => 3),
    'fields' => array('Filter.account_id')
));
$activeAccounts = $this->Account->find('all',array(
    'conditions' => array(
        'Account.account_id'=>$filters,
        'Account.active'=>1
    )
));

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