Drupal Views将Filter转换为CAST()字符串为Float

时间:2015-10-29 14:30:44

标签: mysql drupal drupal-7 views hook

我遇到了一个问题,改变了我的视图,以便它过滤一个字符串字段(我知道只有数字,但不能改变这种情况),以便将其作为数字处理。

normaly我可以通过使用" CAST"来实现这一点。 ...问题是,它在钩子中被改变,所以我的例子如下所示:

fh = open(fname)
for line in fh:
    x = line[22:29]

所以额外的所有字符都被剥离了......: - /

我尝试了很多钩子但是他们似乎都没有做到这一点! 有没有人有想法,我怎么做?! (模块计算字段实际上不是一个选项..)

我目前的代码如下:

... AND (CASTfield_data_field_baserent.field_baserent_valueASDECIMAL <= '1000') ))

其中

function custom_helpers_views_query_alter(&$view, &$query){
if($view->name=="mietangebote"){
    // Miete
        if(!empty($view->exposed_raw_input['field_baserent_value'])){
            foreach($query->where[1]['conditions'] as $key=>$condition){
                if($condition['field']=="field_data_field_baserent.field_baserent_value"){
                    $view->query->where[1]['conditions'][$key]['operator']="<=";
                    $view->query->where[1]['conditions'][$key]['value']=(double)$view->query->where[1]['conditions'][$key]['value'];
                    $view->query->where[1]['conditions'][$key]['field']="CAST(".$view->query->where[1]['conditions'][$key]['field']." AS DECIMAL)";
                    //dpm($view->query->where[1]['conditions'][$key]);
                }
            }
        }
    }
}

是重要的一行

提前感谢:)

3 个答案:

答案 0 :(得分:0)

看看有没有办法自己通过这个条件,如下所示:

$view->query->where[1]['conditions'][$key]->condition("CAST(".$view->query->where[1]['conditions'][$key]['field']." AS DECIMAL) <= " . $view->query->where[1]['conditions'][$key]['value']);

答案 1 :(得分:0)

而不是CAST,只需构建一个0+加到变量和/或常量上的表达式:

0+'1000'

将被视为数字1000,而非字符串 '1000'

答案 2 :(得分:0)

hook_views_query_alter函数应该完成这项工作,它应该是这样的:

function custom_helpers_views_query_alter(&$view, &$query) {
  if ( $view->name == 'mietangebote' ) {

    // Miete
    if(!empty($view->exposed_raw_input['field_baserent_value'])){

      foreach($query->where[1]['conditions'] as $key => $condition) {
        if ( $condition['field'] == 'field_data_field_baserent.field_baserent_value' ) {
          $query->where[1]['conditions'][$key]['operator'] = 'formula';
          $query->where[1]['conditions'][$key]['value'] = array(':val' => (double)$query->where[1]['conditions'][$key]['value']); 
          $query->where[1]['conditions'][$key]['field'] = 'CAST(' . $query->where[1]['conditions'][$key]['field'] . ' AS UNSIGNED) >= :val';
          //dpm($view->query->where[1]['conditions'][$key]);
          break; 
        }
      }

      // For sorting as well
      foreach($query->orderby as $key => $condition) {
        if ( $condition['field'] == 'field_data_field_count_field_count_value' ) {              
          $query->orderby[$key]['field'] = 'CAST(' . $query->orderby[$key]['field'] . ' AS UNSIGNED)'; 
          break; 
        }
      }
    }
  }
}

重要的一点是:

$query->where[1]['conditions'][$key]['operator'] = 'formula';

并允许视图处理&#39;字段&#39;的值作为SQL片段而不是字段的名称。查看add_where_expression的源代码以获取完整详细信息! [doc]

祝你好运!

免责声明:此答案与此question from drupal.stackexchange.com非常类似。修改代码以解决特定问题(不仅仅是复制+粘贴)。