2
該代碼將幾個表連接在一起,然後進行聯合。這是我爲Drupal 7網站編寫的一個模塊。Drupal 7數據庫API'乾淨'代碼
$query1 = db_select('field_data_field_short_title', 's');
$query1->join('node', 'n', 'n.nid = s.entity_id');
$query1->join('field_data_field_a_z', 'a', 'a.entity_id = n.nid');
$query1->addField('s', 'field_short_title_value');
$query1->addField('n', 'nid');
$query1->addField('a', 'field_a_z_value');
$query2 = db_select('field_data_field_short_title_alternative', 'h');
$query2->join('node', 'o', 'o.nid = h.entity_id');
$query2->join('field_data_field_a_z', 'z', 'z.entity_id = o.nid');
$query2->addField('h', 'field_short_title_alternative_value');
$query2->addField('o', 'nid');
$query2->addField('z', 'field_a_z_value');
$query1->union($query2);
$result = $query1->execute();
從考慮使用Drupal的數據庫API的其他例子,給我的感覺有可能寫這個的「清潔」的方式。我的代碼中有很多重複。有更清潔的方法嗎?