drupal sql conditional clauses for multiple tables?

泪湿孤枕 提交于 2019-12-02 19:50:54

问题


I have the below query for FAQ search module modified that works in Drupal 7, which searches in two tables: 1) title 2) body but don't manage to include one more.

$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 . '%%'));

I'd like to add one more to include detailed questions field in the search, but I think the linking for nid is the issue? I tried putting both into a clause but seems to be wrong. Help Please :)

AND (fd.entity_id = fq.nid OR 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;

UPDATE: Thanks to Syscall who helped me make his approach work (see chat).

I also managed to remove empty whitespace (by adding a new variable and using array_filter > I guess the issue was splitting whitespace and trim at the same time that array_map still created empty strings.) and adding a condition to exclude nodes not published via db_and() referencing n.status = 1.

$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. inserting var_dump($variabletodump); was really helpful while changing the code and viewing the effects of the arrays to solve issues.


回答1:


You cannot chain leftJoin (or any join) in Drupal queries, because leftJoin() returns the alias, not the query. Then you should use execute() to "run" the query.

$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()) {
    //..
}

You have to add fields :

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

or

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



回答2:


$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



来源:https://stackoverflow.com/questions/49676437/drupal-sql-conditional-clauses-for-multiple-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!