我有一个drupal db_select 的问题。
这是我的代码:
$query = db_select('node', 'n');
$query->addField('n', 'nid', 'nid');
$query->addField('cfs', 'entity_id', 'feature_support_id');
$query->addField('fpffs', 'entity_id', 'parent_feature_support_id');
$query->addField('cfsfc', 'feature_support_compared_target_id', 'feature_support_compared');
$query->addField('fpffsfc', 'feature_support_compared_target_id', 'parent_feature_support_compared');
//Get feature_support of the feature
$query->join('field_data_feature_support_feature', 'cfs', 'n.nid = cfs.feature_support_feature_target_id');
$query->join('field_data_feature_support_compared', 'cfsfc', 'cfs.entity_id = cfsfc.entity_id');
//Get parent feature_support through feature
$query->join('field_data_feature_parent_feature', 'fp', 'n.nid = fp.entity_id');
$query->join('field_data_feature_support_feature', 'fpffs', 'fp.feature_parent_feature_target_id = fpffs.feature_support_feature_target_id');
$query->join('field_data_feature_support_compared', 'fpffsfc', 'fpffs.entity_id = fpffsfc.entity_id');
$query->condition('n.nid', $node_revision->nid, '=');
$query->condition('cfsfc.feature_support_compared_target_id', 'fpffsfc.feature_support_compared_target_id', '=');
$result = $query->execute();
在数据库中,我的请求应该是
SELECT n.nid AS nid, cfs.entity_id AS feature_support_id, fpffs.entity_id AS parent_feature_support_id, cfsfc.feature_support_compared_target_id AS feature_support_compared, fpffsfc.feature_support_compared_target_id AS parent_feature_support_compared
FROM node n
INNER JOIN field_data_feature_support_feature cfs ON n.nid = cfs.feature_support_feature_target_id
INNER JOIN field_data_feature_support_compared cfsfc ON cfs.entity_id = cfsfc.entity_id
INNER JOIN field_data_feature_parent_feature fp ON n.nid = fp.entity_id
INNER JOIN field_data_feature_support_feature fpffs ON fp.feature_parent_feature_target_id = fpffs.feature_support_feature_target_id
INNER JOIN field_data_feature_support_compared fpffsfc ON fpffs.entity_id = fpffsfc.entity_id
WHERE (n.nid = '9') AND (cfsfc.feature_support_compared_target_id = fpffsfc.feature_support_compared_target_id)
当我在 phpmyadmin 中尝试时,此请求有效,但在 mysql 日志中我有
SELECT n.nid AS nid, cfs.entity_id AS feature_support_id, fpffs.entity_id AS parent_feature_support_id, cfsfc.feature_support_compared_target_id AS feature_support_compared, fpffsfc.feature_support_compared_target_id AS parent_feature_support_compared
FROM node n
INNER JOIN field_data_feature_support_feature cfs ON n.nid = cfs.feature_support_feature_target_id
INNER JOIN field_data_feature_support_compared cfsfc ON cfs.entity_id = cfsfc.entity_id
INNER JOIN field_data_feature_parent_feature fp ON n.nid = fp.entity_id
INNER JOIN field_data_feature_support_feature fpffs ON fp.feature_parent_feature_target_id = fpffs.feature_support_feature_target_id
INNER JOIN field_data_feature_support_compared fpffsfc ON fpffs.entity_id = fpffsfc.entity_id
WHERE (n.nid = '9') AND (cfsfc.feature_support_compared_target_id = 'fpffsfc.feature_support_compared_target_id')
最后看到,在 WHERE 中,'fpffsfc.feature_support_compared_target_id' 周围有单引号,不应该在那里。
这显然是因为 ->condition 的第二个参数似乎只接受变量。任何人都知道我如何使用 db_select 为两个 db 字段创建条件?
谢谢你能给我带来的任何帮助。