查询过滤器将子查询分配给一个字段值

时间:2016-05-04 10:17:02

标签: drupal drupal-7 drupal-views

过去两天我一直坚持这个问题。

请帮忙。

我想分配以下查询。

$qr = "( select subconfigcode.field_subconfigcode_value AS configcode FROM node  node LEFT JOIN  field_data_field_select_parent_configuratio  select_parent_configuratio ON node.nid = select_parent_configuratio.entity_id AND (select_parent_configuratio.entity_type = node AND select_parent_configuratio.deleted = 0) LEFT JOIN  node  select_parent_configuratio_node ON select_parent_configuratio.field_select_parent_configuratio_nid = select_parent_configuratio_node.nid LEFT JOIN  field_data_field_subconfigcode  subconfigcode ON select_parent_configuratio_node.nid = subconfigcode.entity_id AND (subconfigcode.entity_type = 'node' AND subconfigcode.deleted = '0') WHERE (( (select_parent_configuratio.field_select_parent_configuratio_nid = node_field_data_field_select_parent_configuratio.nid) )AND(( (node.status = '1') AND (node.type IN  ('offering')) ))) ORDER BY node.created DESC, configcode ASC LIMIT 1 OFFSET 0)";

一个视图的字段值。

我使用了以下代码。

 function general_views_query_alter(&$view, &$query) {
        $qr = " ( select subconfigcode.field_subconfigcode_value AS configcode FROM node  node LEFT JOIN  field_data_field_select_parent_configuratio  select_parent_configuratio ON node.nid = select_parent_configuratio.entity_id AND (select_parent_configuratio.entity_type = node AND select_parent_configuratio.deleted = 0) LEFT JOIN  node  select_parent_configuratio_node ON select_parent_configuratio.field_select_parent_configuratio_nid = select_parent_configuratio_node.nid LEFT JOIN  field_data_field_subconfigcode  subconfigcode ON select_parent_configuratio_node.nid = subconfigcode.entity_id AND (subconfigcode.entity_type = 'node' AND subconfigcode.deleted = '0') WHERE (( (select_parent_configuratio.field_select_parent_configuratio_nid = node_field_data_field_select_parent_configuratio.nid) )AND(( (node.status = '1') AND (node.type IN  ('offering')) ))) ORDER BY node.created DESC, configcode ASC LIMIT 1 OFFSET 0)";

        $query->add_where(1, "node_field_data_field_select_parent_configuratio__field_data_field_subconfigcode.field_subconfigcode_value",  $qr);

    }

但是,它返回以下查询。

where node_field_data_field_select_parent_configuratio__field_data_field_subconfigcode.field_subconfigcode_value = '( select subconfigcode.field_subconfigcode_value AS configcode FROM node  node LEFT JOIN  field_data_field_select_parent_configuratio  select_parent_configuratio ON node.nid = select_parent_configuratio.entity_id AND (select_parent_configuratio.entity_type = node AND select_parent_configuratio.deleted = 0) LEFT JOIN  node  select_parent_configuratio_node ON select_parent_configuratio.field_select_parent_configuratio_nid = select_parent_configuratio_node.nid LEFT JOIN  field_data_field_subconfigcode  subconfigcode ON select_parent_configuratio_node.nid = subconfigcode.entity_id AND (subconfigcode.entity_type = \'node\' AND subconfigcode.deleted = \'0\') WHERE (( (select_parent_configuratio.field_select_parent_configuratio_nid = node_field_data_field_select_parent_configuratio.nid) )AND(( (node.status = \'1\') AND (node.type IN  (\'offering\')) ))) ORDER BY node.created DESC, configcode ASC LIMIT 1 OFFSET 0) ')

我希望在没有为子查询分配单引号的情况下获取查询,并删除添加到每个值的额外斜线。

请帮忙。

1 个答案:

答案 0 :(得分:0)

答案:

直接查询不适用于add_where条件中的赋值目的。

为此,我使用。

生成了查询对象
function general_views_query_alter(&$view, &$query) {
                   $subQuery = db_select('node', 'node'); $subQuery->leftJoin('field_data_field_select_parent_configuratio', 'select_parent_configuratio ', 'node.nid = select_parent_configuratio .entity_id');
                   $subQuery->condition('select_parent_configuratio.entity_type', 'node', '=');
                   $subQuery->condition('select_parent_configuratio.deleted', '0', '=');
                   $subQuery->leftJoin('node', 'select_parent_configuratio_node',  'select_parent_configuratio.field_select_parent_configuratio_nid  = select_parent_configuratio_node.nid');   
                   $subQuery->leftJoin('field_data_field_subconfigcode', 'subconfigcode',  'select_parent_configuratio_node.nid = subconfigcode.entity_id');    
                   $subQuery->condition('subconfigcode.entity_type', 'node', '=');
                   $subQuery->condition('subconfigcode.deleted', '0', '=');  
                   $subQuery->where("select_parent_configuratio.field_select_parent_configuratio_nid = node_field_data_field_select_parent_configuratio.nid");  
                   $subQuery->condition('node.status', "1", '=');  
                   $subQuery->condition('node.type', array('offering'), 'IN');  
                   $subQuery->orderBy('configcode');
                   $subQuery->addField('subconfigcode', 'field_subconfigcode_value', 'configcode');
                   //$subQuery->range(0, 1);
                   $query->add_where($group,'node_field_data_field_select_parent_configuratio__field_data_field_subconfigcode.field_subconfigcode_value',$subQuery,'in');
}

使用它,我能够生成查询并赋值给变量值。