CakePHP:包含内部连接
正如标题所说,我在查找查询中遇到了连接问题,错误如下:CakePHP:包含内部连接,php,cakephp,cakephp-model,Php,Cakephp,Cakephp Model,正如标题所说,我在查找查询中遇到了连接问题,错误如下: preg_match() expects parameter 2 to be string, array given [CORE/cake/libs/model/behaviors/containable.php, line 301] 我尝试将连接添加到那边的数组中,但没有改变任何东西 这些是我传递给find方法的选项 $options = array( 'contain' => array( 'Answer'
preg_match() expects parameter 2 to be string, array given [CORE/cake/libs/model/behaviors/containable.php, line 301]
我尝试将连接添加到那边的数组中,但没有改变任何东西
这些是我传递给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
)
我想做的是:
每个用户都可以向上/向下投票一个节点(问题/答案/评论)。我想把这些票数加起来。
您应该仅对蛋糕模型关系和可包含性无法完成的事情使用“连接”。我不知道你数据库的细节,但我认为查找操作可以简化。为什么不在这里发布这些表的模式
试试这个:
),,
“条件”=>数组(
'Question.id'=>$id
)
);
您将获得每个答案、注释和注释的投票总数值。(如果尚未添加“hasMany”投票,则可能需要在节点模型中添加该投票)
如果你想获得该问题的总票数,那么我建议:
获取问题的答案和评论列表:
$lvl1=find('list','fields'=>array('id'),'conditions'=>array('Node.parent\u id'=>$id))
然后得到答案的评论列表
$lvl2=find('list','fields'=>array('id'),'conditions'=>array('Node.parent\u id'=>$lvl1))
然后将2个数组组合起来,然后对其求和。您应该只对蛋糕模型关系和可包含性无法完成的事情使用“连接”。我不知道你数据库的细节,但我认为查找操作可以简化。为什么不在这里发布这些表的模式
试试这个:
),,
“条件”=>数组(
'Question.id'=>$id
)
);
您将获得每个答案、注释和注释的投票总数值。(如果尚未添加“hasMany”投票,则可能需要在节点模型中添加该投票)
如果你想获得该问题的总票数,那么我建议:
获取问题的答案和评论列表:
$lvl1=find('list','fields'=>array('id'),'conditions'=>array('Node.parent\u id'=>$id))
然后得到答案的评论列表
$lvl2=find('list','fields'=>array('id'),'conditions'=>array('Node.parent\u id'=>$lvl1))
然后合并2个数组,然后对其求和。添加了模式,您还想知道什么吗?对不起,不起作用。Containeable将“组”视为字段。。。我想我只需要找到所有评论和答案的ID,然后在该数组上进行单独的投票查找。。。谢谢你的帮助;)真正地这很奇怪:添加了模式,还有什么你想知道的吗?对不起,不行。Containeable将“组”视为字段。。。我想我只需要找到所有评论和答案的ID,然后在该数组上进行单独的投票查找。。。谢谢你的帮助;)真正地这很奇怪:所以你想检索:问题,对问题的评论,答案,对答案的评论,以及所有这些元素的投票总数。。在一个查询中查询所有内容。。对我来说似乎很有野心=PNah,我知道我需要对问题、答案和评论进行不同的查询,但我希望将投票加入这些查询…您的$this->votesJoin()
返回一个数组?然后,您不应该再将其包装在一个数组中。此外,您可能应该根据重命名表,这将为您节省一些时间。还可以将每个联接放入一个数组中;)因此,您希望检索:问题、对问题的评论、答案、对答案的评论,以及所有这些元素的投票总数。。在一个查询中查询所有内容。。对我来说似乎很有野心=PNah,我知道我需要对问题、答案和评论进行不同的查询,但我希望将投票加入这些查询…您的$this->votesJoin()
返回一个数组?然后,您不应该再将其包装在一个数组中。此外,您可能应该根据重命名表,这将为您节省一些时间。还可以将每个联接放入一个数组中;)
$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()