1

如何在 PHP 代码中更改某个 SQL 查询表单,例如下面的代码

<?php
function eb_mine_views_query_alter(&$view, &$query) {
  if ($view->name == 'statuser') {
    dsm($query, 'before');
    $query->where[0]['type'] = 'OR';
    dsm($query, 'after');
  }
}
?> 

此代码与 Drupal 修改有关。

上一个查询

SELECT node.title AS node_title, node.nid AS nid, node.created AS node_created
FROM 
{node} node
INNER JOIN {taxonomy_index} taxonomy_index_value_0 ON node.nid = taxonomy_index_value_0.nid AND taxonomy_index_value_0.tid = :views_join_condition_0
INNER JOIN {taxonomy_index} taxonomy_index_value_1 ON node.nid = taxonomy_index_value_1.nid AND taxonomy_index_value_1.tid = :views_join_condition_1
WHERE ((( (taxonomy_index_value_0.tid = :db_condition_placeholder_2) )**AND**( (taxonomy_index_value_1.tid = :db_condition_placeholder_3) )))
ORDER BY node_created DESC

在上面的代码运行结果查询之后

SELECT node.title AS node_title, node.nid AS nid, node.created AS node_created FROM node node OUTER JOIN taxonomy_index taxonomy_index_value_0 ON node.nid = taxonomy_index_value_0.nid AND taxonomy_index_value_0.tid = '9' OUTER JOIN taxonomy_index taxonomy_index_value_1 ON node.nid = taxonomy_index_value_1.nid AND taxonomy_index_value_1.tid = '6' WHERE ((( (taxonomy_index_value_0.tid = '9') )OR( (taxonomy_index_value_1.tid = '6') ))) ORDER BY node_created DESC LIMIT 5 OFFSET 0;

如您所见,查询从 AND 更改为 OR。

现在我想像这样更改相同的代码:

SELECT node.title AS node_title, node.nid AS nid, node.created AS node_created FROM node node LEFT OUTER JOIN taxonomy_index taxonomy_index_value_0 ON node.nid = taxonomy_index_value_0.nid AND taxonomy_index_value_0.tid = '9' LEFT OUTER JOIN taxonomy_index taxonomy_index_value_1 ON node.nid = taxonomy_index_value_1.nid AND taxonomy_index_value_1.tid = '6' WHERE ((( (taxonomy_index_value_0.tid = '9') )OR( (taxonomy_index_value_1.tid = '6') ))) ORDER BY node_created DESC LIMIT 5 OFFSET 0;

而不是使用OUTER JOIN我想使用LEFT OUTER JOIN。那么如何在我的 PHP 代码中执行此操作

4

1 回答 1

0

尝试这个:

<?php
function eb_mine_views_query_alter(&$view, &$query) {
  if ($view->name == 'statuser') {
    dsm($query, 'before');
    $query->where[0]['type'] = 'OR';
    $query->tables[1]['type'] = 'LEFT OUTER';
    dsm($query, 'after');
  }
}
?>
于 2012-10-18T08:50:52.550 回答