7

I'm trying to do a left join but it isn't working.

I have 2 tables:

TABLE profile
- uid
- pid

TABLE field_data_field_naam
- entity_id
- field_naam_value

I want to select the field_naam_value where uid is equal to .. and pid equal to entity_id. My normal query looks like this:

SELECT fdfm.field_naam_value, pf.uid
FROM field_data_field_naam fdfm
LEFT JOIN profile pf
ON fdfm.entity_id=pf.pid
WHERE pf.uid = 4

When I execute this on my database it works. Then I have this:

$naam = db_select('field_data_field_naam', 'fdfm')
     ->fields('fdfm', array('field_naam_value'));
$naam->leftJoin('profile', 'pf', 'fdfm.entity_id = pf.pid');
$naam->condition('uid', 4)
     ->execute();

And when I do the following:

foreach ($naam as $record) {
    die;
}

nothing happens... (he doesn't kill the app). Also tried to add ->fetchAll() but no success.

1 Answer 1

13

Use the below code for fetching values. It will give you field_naam_value field data.

$naam = db_select('profile', 'pf');
$naam->join('field_data_field_naam', 'fd', 'pf.pid = fd.entity_id');
$all_names = $naam->fields('fd', ['field_naam_value'])
  ->condition('pf.uid', 4, '=')
  ->execute()
  ->fetchAll();
foreach ($all_names as $record) {
  die;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.