对具有多个标志的节点的总标志计数对Drupal 6视图进行排序

时间:2012-07-30 20:22:24

标签: php sorting drupal-6 drupal-modules drupal-views

我正在使用Drupal 6并且在我的节点上有多个标志,每个标志都有一个值:有趣,无聊,聪明,有趣,辱骂等等。视图当前能够按每个标志的标志计数排序结果但是我需要按每个音符上所有标志的总组合标志计数对节点进行排序。 'rate'模块非常接近这样做但我需要使用flag模块,这样我就可以使用标志solr模块来允许按标志搜索。我真的不知道从哪里开始......对于我可以采取的方法有什么建议吗?

这是当前的视图查询;

SELECT node.nid AS nid, 
flag_counts_node.count AS flag_counts_node_count,    
flag_counts_node_1.count AS flag_counts_node_1_count, 
flag_counts_node_2.count AS flag_counts_node_2_count, 
flag_counts_node_3.count AS flag_counts_node_3_count, 
flag_counts_node_4.count AS flag_counts_node_4_count FROM node node  
LEFT JOIN flag_counts flag_counts_node ON node.nid = flag_counts_node.content_id AND flag_counts_node.fid = 7 
LEFT JOIN flag_counts flag_counts_node_1 ON node.nid = flag_counts_node_1.content_id AND flag_counts_node_1.fid = 6 
LEFT JOIN flag_counts flag_counts_node_2 ON node.nid = flag_counts_node_2.content_id AND flag_counts_node_2.fid = 4 
LEFT JOIN flag_counts flag_counts_node_3 ON node.nid = flag_counts_node_3.content_id AND flag_counts_node_3.fid = 5 
LEFT JOIN flag_counts flag_counts_node_4 ON node.nid = flag_counts_node_4.content_id AND flag_counts_node_4.fid = 3 
WHERE (node.type in ('rpodcast', 'upodcast')) AND (node.status = 1) ORDER BY flag_counts_node_count ASC, flag_counts_node_1_count ASC, flag_counts_node_2_count ASC, flag_counts_node_3_count ASC, flag_counts_node_4_count ASC

目前为止的模块代码;

<?php
function flagcountview_views_pre_execute(&$view){
if ($view->name == 'audio') {
$sql = $view->build_info['query']; 
$flag_count = "flag_counts_node.count + flag_counts_node_1.count +  flag_counts_node_2.count + flag_counts_node_3.count"; 
$sql .= "ORDER BY $flag_count DESC"; 
$view->build_info['query'] = $sql;
}
}

2 个答案:

答案 0 :(得分:2)

您似乎无法添加字段,因此我们可以尝试替代方法

function flagcountview_views_pre_execute(&$view){
  if ($view->name == 'audio') {
    $sql = $view->build_info['query']; 
    $search = "ORDER BY flag_counts_node_count ASC, flag_counts_node_1_count ASC, flag_counts_node_2_count ASC, flag_counts_node_3_count ASC, flag_counts_node_4_count ASC";
    $replace = "ORDER BY flag_counts_node.count + flag_counts_node_1.count +  flag_counts_node_2.count + flag_counts_node_3.count ASC"; 
    $sql = str_replace($search, $replace, $sql);; 
    $view->build_info['query'] = $sql;
  }
}

确保您在视图中添加了所有标记计数 - &gt;排序标准:)

答案 1 :(得分:1)

考虑到你已经在视图中添加了所有标志关系和标志计数,最好使用预执行视图来添加所有计数和排序

更新

希望你已经在relatioship和fields中添加了所有标志数。现在在代码中添加'

function MODULENAME_views_pre_execute(&$view){
  $sql = $view->build_info['query']; 
  $flag_count = flag_counts_node_count + flag_counts_node_1_count + flag_counts_node_2_count + flag_counts_node_3_count + flag_counts_node_4_count; 
  $sql .= " ORDER BY $flag_count ASC"; 
  $view->build_info['query'] = $sql;
}

重要

  • 确保您从“排序条件”中删除了所有内容
  • 确保您已添加 'fields'中的所有标志计数