多个表的drupal sql条件子句?

时间:2018-04-05 15:42:35

标签: mysql sql drupal drupal-7

我在以下查询中修改了在Drupal 7中运行的FAQ搜索模块, 它在两个表中搜索:1)标题2)正文但不能再包括一个。

$term = strip_tags(drupal_substr($_POST['keyword'], 0, 100));

$query = "SELECT DISTINCT fq.title, fq.nid
 FROM {node} AS fq, {field_data_body} AS f
 WHERE fq.title LIKE :term
 OR fq.type LIKE :term
 OR f.body_value LIKE :term
 AND f.entity_id = fq.nid";

$result = db_query($query, array(':term' => '%%' . $term . '%%',':term' => '%%' . $term . '%%',':term' => '%%' . $term . '%%'));

我想再添加一个在搜索中包含详细问题字段,但我认为nid的链接是问题吗?我尝试将两者都放入一个条款但似乎是错误的。请帮助:)

  

AND(fd.entity_id = fq.nid或fb.entity_id = fq.nid)“;

$term = strip_tags(drupal_substr($_POST['keyword'], 0, 100));
 $query = "SELECT DISTINCT fq.title, fq.nid
    FROM {node} AS fq, {field_data_field_detailed_question} AS fd, {field_data_body} AS fb
    WHERE fq.title LIKE :term
    OR fd.field_detailed_question_value LIKE :term
    OR fb.body_value LIKE :term
    AND (fd.entity_id = fq.nid OR fb.entity_id = fq.nid)";
 $result = db_query($query, array(':term' => '%%' . $term . '%%',':term' => '%%' . $term . '%%',':term' => '%%' . $term . '%%'));

 $string = "";
 while ($row = $result->fetchObject()) {
    $string .= "<a href='/" . drupal_get_path_alias('node/' . $row->nid) . "'>" . $row->title . "</a>"; }
echo $string;

更新:感谢Syscall帮助我开展工作(请参阅聊天)。

我还设法删除了空白空格(通过添加一个新变量并使用array_filter&gt;我想问题是在array_map仍然创建空字符串的同时拆分空格和修剪。)并添加条件以排除节点而不是通过引用n.status = 1的db_and()发布。

    $terms = explode(' ', $term); // split using ' '
    $terms = array_map('trim', $terms); // remove unwanted spaces
     $termsfiltered = array_filter($terms);
    $or = db_or();
        foreach ($termsfiltered as $term) {
           $or->condition('fd.field_detailed_question_value', '%'.db_like($term).'%', 'LIKE');
            $or->condition('fb.body_value','%'.db_like($term).'%' , 'LIKE');
            $or->condition('n.title','%'.db_like($term).'%' , 'LIKE');
        }
        $and = db_and()->condition('n.status','1' , 'LIKE');

$query = db_select('node', 'n');
$query->fields('n');
$query->leftJoin('field_data_body' , 'fb', 'fb.entity_id=n.nid');
$query->leftJoin('field_data_field_detailed_question' ,'fd', 'fd.entity_id=n.nid');
$query->condition($or);
$query->condition($and);
$stmt = $query->execute(); // execute the query (returns the "statement" to fetch).

P.S。插入 var_dump($ variabletodump); 在更改代码和查看数组效果以解决问题时非常有用。

2 个答案:

答案 0 :(得分:1)

$or = db_or()
  ->condition('fd.field_detailed_question_value', '%'.db_like($term ).'%', 'LIKE')
  ->condition('fb.body_value','%'.db_like($term ).'%' , 'LIKE');

$results = db_select('node', 'n')
  ->fields('n', array('nid', 'title'))
  ->leftJoin('field_data_body' , 'fb', 'fb.entity_id=n.nid')
  ->leftJoin('field_data_field_detailed_question' ,'fd', 'fd.entity_id=n.nid')
  ->condition($or)
  ->execute()
  ->fetchAll();

var_dump($results);

db_select documentation

db_or documentation

答案 1 :(得分:1)

您无法在Drupal查询中链接leftJoin(或任何join),因为leftJoin()会返回别名,而不是查询。然后,您应该使用execute()来“运行”查询。

$or = db_or()
  ->condition('fd.field_detailed_question_value', '%'.db_like($term ).'%', 'LIKE')
  ->condition('fb.body_value','%'.db_like($term ).'%' , 'LIKE');

$query = db_select('node', 'n');
$query->fields('n');
$query->leftJoin('field_data_body' , 'fb', 'fb.entity_id=n.nid');
$query->leftJoin('field_data_field_detailed_question' ,'fd', 'fd.entity_id=n.nid');
$query->condition($or);
$stmt = $query->execute(); // execute the query (returns the "statement" to fetch).

while ($row = $stmt->fetchObject()) {
    //..
}

您必须添加字段:

$query = db_select('node', 'n')->fields('n');

$query = db_select('node', 'n')
         ->addField('n','title')
         ->addField('n','nid');