dtbl1231 2011-07-14 17:33
浏览 49

CakePHP:加入内部包含

As the title says, I'm having troubles with joins in my find-query, with errors appearing like:

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

I've tried adding joins to the array over there, but it didn't change anything.

These are the options I'm passing to the find method.

$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);

with votesJoin() returning the following array.

(
   [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
)

What I'm trying to do: Each user can up/downvote a node (question/answer/comment). With the join I'm trying to add the sum of those votes. database http://github.com/navale/QA/wiki/img/datamodel.png

  • 写回答

1条回答 默认 最新

  • doudou20145 2011-07-15 04:40
    关注

    You should use "joins" only for things that can't be done with Cake model relationship and Containable. I don't know the details of your database, but I think the find operation can be simplified. Why don't you post the schema for these tables on here?

    try this:

    $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()
    

    ), 'conditions' => array( 'Question.id' => $id ) );

    You get the Vote sum value for each answer, comment, and comment for answer. (You might need to add 'hasMany' Vote in the Node model if you haven't done that yet)

    If instead you want to get one single total sum of Vote for the question, then I'd suggest: get the list of the answers and comments of the question:

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

    then get list of the comments of the answers

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

    then just combine the 2 array then do a sum over that.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?