视图hook_views_query_alter加入

时间:2016-04-01 15:55:27

标签: join drupal-7 drupal-views

在hook_views_query_alter中,我建立这样的连接查询:

$join = new views_join();
$join->table = 'table_2';
$join->field = 'field_2';
$join->left_table = 'table_1';
$join->left_field = 'field_1';
$join->type = 'LEFT';
$join->extra = array(
  array(
     'table' => 'table_2',
     'field' => 'field_3',
     'value' => 'table_1.field_4',
  ),
);

查询应如下所示:

LEFT JOIN {table_2} table_2 ON table_1.field_1 = table_2.field_2 AND table_1.field_3 = table_2.field_4

我的问题在这里'value'=> 'table_1.field_4'即可。我不能将值设置为字段。它将其视为字符串值。所以在我和我的查询中,我有不需要的单引号。

所以最后我的查询看起来像这样:

LEFT JOIN {table_2} table_2 ON table_1.field_1 = table_2.field_2 AND table_1.field_3 = 'table_2.field_4'

1 个答案:

答案 0 :(得分:0)

我不确定它是否是最好的解决方案,但是:

$join = new views_join();
$join->table = 'table_2';
$join->field = 'field_2';
$join->left_table = 'table_1';
$join->left_field = 'field_1';
$join->type = 'LEFT';

$join->extra = 'table_1.field_3 = table_2.field_4';

适合我。