1

I have just learnt how to use the EntityFieldQuery to make simple queries.

In the example below I fetch all the comment id's for the logged-in-user.

global $user;

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'comment')
->propertyCondition('uid', $user->uid)
->addMetaData('account', user_load(1));
$result = $query->execute();
var_dump($result);
$cids = array();
if (isset($result['comment']))
$cids = array_keys($result['comment']);

Is it possible to get the comment body and the node id where that comment belongs using the way i use above?

2 Answers 2

1

No - you need to load the comment (comment_load()) to get property and field data attached to the results of an EFQ - its job is to load IDs only.

An alternative would be mikeytown2's EntityFieldQueryExtra class, which does allow you to attach field and property data to the results of the query.

0

To fetch a type of entity you have to add the entity_type condition to it.

You have to fetch nodes, so the code for it will be as follows:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node');

The entity condition is set using the function entityCondition in which you can set the entity type as node. Once if you have set the entity condition, you can execute the query. The node ids are returned in an array in the node key of the result. The complete code to display the nodes will be as follows:

$query = new EntityFieldQuery();
    $query
    ->entityCondition('entity_type', 'node');


    $result = $query->execute();
    $nodes = array();
    if (isset($result['node'])) {
      $nids = array_keys($result['node']);
      $nodes = node_load_multiple($nids);
    }

    $list = array();
    foreach ($nodes as $node) {
      $options = array('absolute' => TRUE);
      $url = url('node/' . $node->nid, $options);
      $list[] = '<a href='.$url.'>'.$node->title.'</a>';
    }

    $theme_args = array('items' => $list, 'type' => 'ol');
    $content = theme('item_list', $theme_args);

    $block = array(
      'subject' => t('A block to display results from entityquery'),
      'content' => $content,
    );
    return $block;

Thank you.

5
  • This is completely wrong - the OP is asking how to load comment entities, there's no mention of nodes.
    – Clive
    Commented Nov 14, 2014 at 14:15
  • You've lost me - the question is Is it possible to get the comment body and the node id where that comment belongs using the way i use above?. The answer is no, it doesn't involve loading nodes
    – Clive
    Commented Nov 14, 2014 at 14:27
  • How could you say that this answer is completely wrong? I took this question when I saw the title "Fetching more than id's of an entity using EntityFieldQuery". Was I true about it? And now the body: if the answer explains the basic rule-then why not comment and comment body? I don't think anyone here seeks for their first attempt in D7(esp. when trying entities). If you think it was not directly meant to the question, let me bow, I am very sorry. Commented Nov 14, 2014 at 14:32
  • No need to apologise, your opinion is just as valid as mine :) I just don't think this addresses the specific question that was asked
    – Clive
    Commented Nov 14, 2014 at 14:34
  • That was a mis-edit. Really sorry. :) Have a nice day. Commented Nov 14, 2014 at 14:35

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.