此代码将几个表连接在一起,然后进行联合。这是我为 Drupal 7 站点编写的一个模块。
$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 的其他示例,我觉得可能有一种“更简洁”的编写方式。我的代码中有很多重复。有没有更清洁的方法?