CakePHP:在ContainableBehaviour中选择DISTINCT

时间:2011-05-13 10:00:39

标签: cakephp distinct containable

我试图只选择不同的相关模型条目,但它似乎不起作用。

我有这个:

$active_questions = $this->Question->find('all', array('conditions' => array('test_id' => $active_tests), 'fields' => array('answer_style_id'), 'contain' => array(
        'Answer' => array(
            'fields' => array('capital_category_id'),
            'CapitalCategory' => array(
                'fields' => array('id', 'DISTINCT capital_id', 'DISTINCT category_id', 'delete_flag'),
                'Capital' => array(
                    'fields' => array('id', 'delete_flag')
                ),
                'Category' => array(
                    'fields' => array('id', 'delete_flag')
                )
            )
        )
    )));

但是Cake似乎会自动添加相关的模型键,即使我使用DISTINCT关键字指定了它:

Query: SELECT `CapitalCategory`.`id`, DISTINCT `CapitalCategory`.`capital_id`, DISTINCT `CapitalCategory`.`category_id`, `CapitalCategory`.`delete_flag`, `CapitalCategory`.`capital_id`, `CapitalCategory`.`category_id` FROM `capital_categories` AS `CapitalCategory`   WHERE `CapitalCategory`.`id` = 217  

如何仅滤除DISTINCT大写字母或类别?对于当前示例,Cake返回具有相同id的20个类别。我只想要一个人回来。

谢谢。

2 个答案:

答案 0 :(得分:0)

在我的头顶,以下可能使用'group'选项

$active_questions = $this->Question->find(
  'all', 
  array(
    'conditions' => array('Question.test_id' => $active_tests), 
    'fields' => array('answer_style_id'), 
    'contain' => array(
      'Answer' => array(
        'fields' => array('capital_category_id'),
        'CapitalCategory' => array(
          'fields' => array('id', 'capital_id', 'category_id', 'delete_flag'),
          'group' => array('capital_id', 'category_id'),
          'Capital' => array(
            'fields' => array('id', 'delete_flag')
          ),
          'Category' => array(
            'fields' => array('id', 'delete_flag')
          )
        )
      )
    )
  )
);

答案 1 :(得分:0)

为什么不将'group'放在'fields'下面而不是'contains'中,你也可能必须完全删除'fields'。