【问题标题】:CakePHP: joins inside containCakePHP:加入包含
【发布时间】:2012-08-26 02:56:32
【问题描述】:

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

  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
)

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

【问题讨论】:

  • 所以你想检索:问题、问题上的 cmets、答案、答案上的 cmets,以及所有这些元素的投票总和......以及一个查询中的所有内容..对我来说似乎很有野心=P
  • 不,我知道我需要对问题、答案和 cmets 进行不同的查询,但我加入对这些查询的投票...
  • 你的$this->votesJoin()返回一个数组?那么你不应该将它包装在另一个数组中。
  • 另外,您可能应该根据Cake conventions 重命名您的表格,这样可以节省一些头发。
  • CakePHP website 也将每个连接放入一个数组中;)

标签: php cakephp cakephp-model


【解决方案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()

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

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

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

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

然后得到答案的cmets列表

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

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

【讨论】:

  • 添加了架构,还有什么想知道的吗?
  • 抱歉,没用。 Containable 将“组”视为一个字段...我想我会得到所有 cmets 的 ID 和找到的答案,然后在该数组上进行单独的投票...尽管感谢您的帮助 ;)跨度>
猜你喜欢
  • 2014-08-30
  • 2017-10-08
  • 1970-01-01
  • 2011-07-29
  • 2019-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-16
相关资源
最近更新 更多