0

I have 2 content types:

  1. vehicles
  2. engines

Several engine nodes are referenced to the single vehicle. Is there any way I can load all engines referenced to the single vehicle node if I have that vehicle node object (thus the nid as well)?

I use Node reference module.

If you need any more info, please ask. Thanks in advance!

  • If you switch to Entity reference my earlier response today might be helpfulhttp://drupal.stackexchange.com/questions/124893/what-is-the-difference-between-referenced-entity-and-referencing-entity-in-views – Pontus Nilsson Jul 28 '14 at 17:26
  • Using Reverse Node Reference maybe? Have you searched before you asked? If so, why this module does not help? – Mołot Jul 29 '14 at 6:56
  • @Mołot of course I've searched before I asked. This module doesn't help because it is for Drupal 6 solely. I know I can do a reverse reference from Views, so I suppose there is a way to do it programmatically. – Srdjan Marjanovic Jul 29 '14 at 7:53
1

I found a solution to this issue by performing a query on 'field_data_field_vehicle_reference' table.

For my case, the solution can be something like this:

function engines_of_vehicle($vehicle) {
  $nid = $vehicle->nid;

  // get all engine nids referenced to this vehicle
  $engine_nids = db_query('SELECT ref.entity_id FROM {field_data_field_vehicle_reference} ref WHERE ref.field_vehicle_reference_nid  =:nid AND ref.bundle =:bundle', array(':nid' => $nid, ':bundle' => 'engine'))->fetchCol();

  //load all engines referenced to this nid
  $engines = node_load_multiple($engine_nids);

  return $engines;    
}

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.