3

How to use EntityFieldQuery to load the nodes based on available nids. I tried,

$query->propertyCondition('nid', $nid);

and

$query->entityCondition('nid', $nid);

Both of the above didn't work. What would be the right way?

5

There's no point doing that, use node_load() or node_load_multiple() instead (or entity_load()). EFQ exists to return you the IDs of entities, essentially so you can use them in an entity load function. If you already know the ID beforehand, then using EFQ is a waste of a database query.

To illustrate, if you were to add a nid condition to your query, then these:

->entityCondition('bundle', 'article') // ex. article
->propertyCondition('uid', $uid) // node written by a specific user
->propertyCondition('status', 1);

are all redundant, your query will just be:

$query->entityCondition('entity_type', 'node')->propertyCondition('nid', $nid);

and the only useful data you'll get is the node ID, which you already have.

1

Probably $query->propertyCondition('nid',$nid); will work for you. I use same to fetch node like.

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
      ->entityCondition('bundle', 'article') // ex. article
      ->propertyCondition('uid', $uid) // node written by a specific user
      ->propertyCondition('status', 1); // published nodes
$result = $query->execute();
  • 1
    means you looking for multiple nodes? – vgoradiya Sep 24 '15 at 13:16
  • 1
    Multiple nodes with the same ID? That's not possible... – Clive Sep 24 '15 at 13:17
  • 2
    that can be possible with $nids = array(1,2,3); node id and in propertyCondition pass $query->propertyCondition('nid', $nids, 'IN') – vgoradiya Sep 24 '15 at 13:28
  • 1
    Possible, yes, but also a complete waste of time and resources. All an EFQ can return to you are the node IDs. If you already have the node IDs, then you've just wasted a trip to the database for precisely nothing. – Clive Sep 24 '15 at 13:34
  • yes, thats true. – vgoradiya Sep 24 '15 at 13:51
0

I think it depends. If your entity field query is only using entity property conditions (the columns in the node table), then you can use nid.

If you add a field condition, entity field query will change it's base table (e.g. the table referenced in the FROM part of the from part of the query to that table). In which case, since most fields are generalized between entities, and refer to ids as "entity_id" instead of "nid" or "uid", you will need to use that column name instead.

I have not tested this with just accessing entity properties, but I'm looking at an EFQ right now that's failing because I use "nid" instead of "entity_id".

0

You can use it like

$query = new EntityFieldQuery();
$query->entityCondition('entity_type','node');
$query->propertyCondition('status', 1);
$query->propertyOrderBy('title', 'ASC');
$query->entityCondition('bundle','committee_tenure');
$query->propertyCondition('nid',$member_com_ids,'IN');
$result = $query->execute();
-1

You only need to pass a node ID to propertyCondition.

$query->propertyCondition('nid', 886);
  • OP tried that and it didn't work. Like they wrote in their question. So... – leymannx Nov 5 '18 at 8:31

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.