正如标题所说,我在查找查询中遇到了连接问题,错误出现如下:
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
)
我要做的是:每个用户都可以对一个节点(问题/答案/评论)进行上/下投票。在join中,我试图将这些投票的总和相加。数据库http://github.com/navale/QA/wiki/img/datamodel.png
你应该使用"join "只用于那些不能用Cake模型关系和Containable完成的事情。我不知道你们数据库的细节,但是我认为查找操作可以简化。你为什么不把这些表的模式贴在这里呢?
试试这个:
$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("的问题。Id ' => $ Id)
);
您将获得每个答案、评论和对答案的评论的投票总和值。(如果你还没有这样做,你可能需要在Node模型中添加'hasMany' Vote)
如果你想获得一个问题的总票数,那么我建议:获取问题的答案和评论列表:
$ lvl1 =找到("列表","字段"=>数组(id),"条件"=>数组(' Node.parent_id ' => id)美元)
然后得到答案的评论列表
$ lvl2 =找到("列表","字段"=>数组(id),"条件"=>数组(' Node.parent_id ' => lvl1)美元)
然后将两个数组合并然后对其求和