0

$result = $query->entityCondition('entity_type', 'node')

->entityCondition('bundle', 'mypage')
->fieldCondition('field_mymodule_module','tid', 123,'=')
->execute();

$nids = array_keys($result['node']);

$or = db_or();

foreach($nids as $nid)

$or->condition('m.entity_id', $nid, '=');

$query2 = db_select('metatag', 'm')

->fields('m', array('entity_id', 'revision_id', 'language', 'data')) ->condition('m.entity_type', 'node') ->condition($or) ->orderBy('entity_id') ->orderby('revision_id') ->execute();

while ($record = $query2->fetchAssoc()) {

dpm($record);

}

As you can see in above code, I'm trying to get metadata information of each node. Getting nids of them turns out to be easy. However, when I use those nids to retrieve metadata, I get so many identical ones. That problem persists even after I removed "or" statement from the db_select and hard-coded entity_id, it returns like 10 identical records. Can anyone shed a light on the problem and where I am doing wrong here?

8
  • $or->condition('m.entity_id', $nids, '='); you are using $nids in this.use $nid in place of $nids Aug 1, 2014 at 13:10
  • You are right but as I said in my post, I have dropped the whole "or" statement together and hardcoded the nid but it still returns redundant records. I just tested with db_query and constructed my own SQL statement just to make sure I'm not using db_select wrong, but the problem persists.
    – KMC
    Aug 1, 2014 at 13:15
  • You got all all nodes id in $nids. have you checked that $nids contains unique nodes id Aug 1, 2014 at 13:17
  • I did (with devel's dpm), there's only 11 of them and they are all unique.
    – KMC
    Aug 1, 2014 at 13:19
  • This happens because in foreach you are using while loop. and it runs 11 times Aug 1, 2014 at 13:29

1 Answer 1

0

$result = $query->entityCondition('entity_type', 'node')

        ->entityCondition('bundle', 'mypage')
        ->fieldCondition('field_mymodule_module','tid', 123,'=')
        ->execute();

    $nids = array_keys($result['node']);
    $array=array();

    foreach($nids as $nid)

{
    $query2 = db_select('metatag', 'm')

    ->fields('m', array('entity_id', 'revision_id', 'language', 'data')) ->condition('m.entity_type', 'node') 

->condition('m.entity_id', $nid, '=') ->orderBy('entity_id') ->execute()->fetchAssoc();

$array[]=$query2 

        }

$array contains all fields value.traverese this array.
Hope this will give you result.

Your Answer

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

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