drupal sql conditional clauses for multiple tables?

前端 未结 2 639
萌比男神i
萌比男神i 2021-01-26 01:47

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.



        
相关标签:
2条回答
  • 2021-01-26 02:22
    $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

    0 讨论(0)
  • 2021-01-26 02:36

    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');
    
    0 讨论(0)
提交回复
热议问题