1

I want join to my select three fc fields:

  • field_data_field_show_block_carousel
  • field_data_field_poster_film
  • field_data_field_film_link

I try this:

$nids = db_select('node', 'n');
  $nids->join(array('field_data_field_show_block_carousel', 'field_data_field_poster_film', 'field_data_field_film_link',), 'fc', 'n.nid = fc.entity_id');
  $nids->fields('fc', array('field_poster_film_fid','field_film_link_url',));
  $nids->fields('n', array('nid', 'title', 'uid',))
    ->condition('n.type', 'vodeo_library')
    ->condition('fc.field_show_block_carousel_value', 1)
    ->execute(); // returns an indexed array
  $result = $nids->execute()->fetchAllAssoc('nid');

but recived sql error? pls tell me what I do wrong?

1

You should try to write each join on a new line, not as an array. See Joins and public function SelectQuery::join.

So try it like this:

$nids = db_select('node', 'n');
  $nids->join('field_data_field_show_block_carousel', 'fc', 'n.nid = fc.entity_id');
  $nids->join('field_data_field_poster_film', 'fp', 'n.nid = fp.entity_id');
  $nids->join('field_data_field_film_link', 'fl', 'n.nid = fl.entity_id');
  $nids->fields('fp', array('field_poster_film_fid'));
  $nids->fields('fl', array('field_film_link_url'));
  $nids->fields('n', array('nid', 'title', 'uid',))
    ->condition('n.type', 'vodeo_library')
    ->condition('fc.field_show_block_carousel_value', 1)
    ->execute(); // returns an indexed array
  $result = $nids->execute()->fetchAllAssoc('nid');

Depending on what your module attempts to achieve, you might also want to have a look at the default Drupal functions for attaching fields to nodes (you only have a condition on one field):

You can get a rendered field value like this:

$node = node_load($nid);
$poster= field_get_items('node', $node, 'field_data_field_poster_film');
$output = field_view_value('node', $node, 'field_data_field_poster_film', $poster[0]);

And if you don't mind a little overhead, you might want to try an EntityFieldQuery. It will give you all fields and is a little slower then a regular SQL query, but is much easier to read and change.

$query = new EntityFieldQuery();

$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'vodeo_library')
  ->propertyCondition('status', NODE_PUBLISHED)
  ->fieldCondition('field_show_block_carousel_value', 'value', 1, '=');

$result = $query->execute();

if (isset($result['node'])) {
  $news_items_nids = array_keys($result['node']);
  $news_items = entity_load('node', $news_items_nids);
}
|improve this answer|||||
0

Please use following query.

Note: Change table alias in $nids->fields('fc', array('field_poster_film_fid','field_film_link_url')); accordingly.

$nids = db_select('node', 'n');
$nids->join('field_data_field_show_block_carousel', 'fc', 'fc.entity_id = n.nid');
$nids->join('field_data_field_poster_film', 'ff', 'ff.entity_id = n.nid');
$nids->join('field_data_field_film_link', 'fl', 'fl.entity_id = n.nid');
$nids->fields('fc', array('field_poster_film_fid','field_film_link_url'));
$nids->fields('n', array('nid', 'title', 'uid'))
    ->condition('n.type', 'vodeo_library')
    ->condition('fc.field_show_block_carousel_value', 1, '=')
    ->execute(); // returns an indexed array
$result = $nids->execute()->fetchAllAssoc('nid');
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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