Search code examples
phpmysqlsqldrupaldrupal-7

With join what must be the fields in order to take all the values?


 $query = db_select('watchdog', 'wa');
 $query->leftJoin('malgona', 'm', 'm.wid = wa.wid')
       ->fields('m');

If I put in fields m I only take the values of m and if I put wa I only take the values of wa. What must be the fields value in order to take the join (with values from both tables)?


Solution

  • You can call the fields method several times:

     $query = db_select('watchdog', 'wa');
     $query->leftJoin('malgona', 'm', 'm.wid = wa.wid')
        ->fields('wa')
        ->fields('m');
    

    It is probably better if you also define the columns for the tables as using this generic fields without column definitions will result in name collision (see Drupal:Fields) if you have same named columns in the tables:

     $query = db_select('watchdog', 'wa');
     $query->leftJoin('malgona', 'm', 'm.wid = wa.wid')
        ->fields('wa', array('wa_id', 'wa_field1', 'wa_field2'))
        ->fields('m', array('m_id', 'm_field1', 'm_field2'));