1

正如标题所说,我在查找查询中遇到了连接问题,错误如下所示:

  preg_match() expects parameter 2 to be string, array given [CORE/cake/libs/model/behaviors/containable.php, line 301]

我试过添加joins到那里的数组中,但它没有改变任何东西。

这些是我传递给 find 方法的选项。

$options = array(
   'contain' => array(
      'Answer' => array(
         'conditions' => array('Answer.type' => 'answer'),
         'joins' => array(
            $this->votesJoin()
         ),
         'Comment' => array(
            'conditions' => array('Comment.type' => 'comment'),
            'joins' => array(
               $this->votesJoin()
            )
         )
      ),
      'Comment' => array(
         'conditions' => array('Comment.type' => 'comment'),
         'joins' => array(
            $this->votesJoin()
         )
      ),
      'User',
      'Tag' => array()
   ),
   'joins' => array(
      $this->votesJoin()
   ),
   'conditions' => array(
      'Question.id' => $id
   )
);

return $this->find('first', $options);

返回以下votesJoin()数组。

(
   [table] => (SELECT Vote.node_id, SUM(value) as total FROM votes AS `Vote`   WHERE 1 = 1  GROUP BY `Vote`.`node_id`  )
   [alias] => Vote
   [conditions] => Vote.node_id = Question.id
)

我正在尝试做的事情:每个用户都可以对一个节点投赞成票/反对票(问题/答案/评论)。通过加入,我试图添加这些选票的总和。 数据库 http://github.com/navale/QA/wiki/img/datamodel.png

4

1 回答 1

1

您应该仅将“joins”用于无法通过 Cake 模型关系和 Containable 完成的事情。我不知道你的数据库的细节,但我认为find操作可以简化。为什么不在这里发布这些表的架构?

尝试这个:

$options = array(
'contain' => array(
  'Answer' => array(
     'conditions' => array('Answer.type' => 'answer'),
     'Vote' => array(
        'fields' => array('SUM(Vote.value)'),
        'group'  => array('Vote.parent_id')
     ),
     'Comment' => array(
        'conditions' => array('Comment.type' => 'comment'),
        'Vote' => array(
                        'fields' => array('SUM(Vote.value)'),
                        'group'  => array('Vote.parent_id')
                 )
     )
  ),
  'Comment' => array(
     'conditions' => array('Comment.type' => 'comment'),
     'Vote' => array(
        'fields' => array('SUM(Vote.value)'),
        'group'  => array('Vote.parent_id')
     )
  ),
  'User',
  'Tag' => array()

), '条件' => 数组('Question.id' => $id ) );

您将获得每个答案、评论和答案评论的投票总和值。(如果您还没有这样做,您可能需要在节点模型中添加“hasMany”投票)

相反,如果您想为该问题获得一个总投票数,那么我建议:获取该问题的答案和评论列表:

$lvl1 = find('list','fields'=>array('id'),'conditions'=>array('Node.parent_id'=>$id))

然后获取答案的评论列表

$lvl2 = find('list','fields'=>array('id'),'conditions'=>array('Node.parent_id'=>$lvl1))

然后只需组合 2 个数组,然后对其进行求和。

于 2011-07-15T04:40:04.440 回答