4

I have a problem with a drupal db_select.

Here is my code :

    $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();

In DB my request should be

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)

This request work when I try it in phpmyadmin, but instead in mysql log I have

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')

See at the end, in the WHERE, there is single quote around 'fpffsfc.feature_support_compared_target_id' which should not be there.

It's obviously because the second argument of ->condition seems only accept variable. Anyone know how I can make a condition with two db fields with db_select?

Thanks for any help you can bring me.

yburon
  • 151
  • 2
  • 11
  • 1
    You're probably looking for [`SelectQuery::where`](http://api.drupal.org/api/drupal/includes!database!select.inc/function/SelectQuery%3A%3Awhere/7) – Clive Oct 17 '12 at 21:43

1 Answers1

2

Use $query->where($snippet, $args = array());

$query->where('cfsfc.feature_support_compared_target_id = fpffsfc.feature_support_compared_target_id');  
Vidyadhar
  • 1,088
  • 9
  • 15