CakePHP 2.x为我的查询添加不需要的条件规则

时间:2019-02-01 07:11:34

标签: cakephp cakephp-2.0

我正在一个运行CakePHP 2.x的“旧”网站上工作。我需要做的是显示与特定练习有关的所有试验。

所以,这是我的发现(从ObedienceExercise模型执行):

$items = $this->find('all', array(
    'order' => array(
        'obedience_class_id' => 'ASC',
        'order' => 'ASC'
    ),
    'contain' => array(
        'ObedienceTrial' => array(
            'conditions' => array(
                'ObedienceTrial.event_id' => $event_id
            )
        )
    )
));

问题是生成的SQL查询包含的条件比我实际需要的条件更多:

SELECT `ObedienceTrial`.`id`, `ObedienceTrial`.`event_id`, `ObedienceTrial`.`obedience_exercise_id`, `ObedienceTrial`.`obedience_class_id`, `ObedienceTrial`.`judge1`, `ObedienceTrial`.`judge2`, `ObedienceTrial`.`judge3`, `ObedienceTrial`.`inspector1`, `ObedienceTrial`.`inspector2`, `ObedienceTrial`.`inspector3`, `ObedienceTrial`.`ring`, `ObedienceTrial`.`order` 
FROM `enci`.`obedience_trials` AS `ObedienceTrial` 
WHERE `ObedienceTrial`.`event_id` = 1374 
AND `ObedienceTrial`.`id` IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49)

我需要的是没有AND条件的相同查询,所以

SELECT `ObedienceTrial`.`id`, `ObedienceTrial`.`event_id`, `ObedienceTrial`.`obedience_exercise_id`, `ObedienceTrial`.`obedience_class_id`, `ObedienceTrial`.`judge1`, `ObedienceTrial`.`judge2`, `ObedienceTrial`.`judge3`, `ObedienceTrial`.`inspector1`, `ObedienceTrial`.`inspector2`, `ObedienceTrial`.`inspector3`, `ObedienceTrial`.`ring`, `ObedienceTrial`.`order` 
FROM `enci`.`obedience_trials` AS `ObedienceTrial` 
WHERE `ObedienceTrial`.`event_id` = 1374 

我在做什么错?仅在ObedienceTrial中设置event_id字段,而ObedienceExercise是练习的常规列表,与Event无关。

0 个答案:

没有答案