0

What is the best way to do a db query for fields in a content type?

I need to get specific fields from a content type, but only where the 'featured' field value is 1.

<?php
        $query = new EntityFieldQuery();

        $query->entityCondition('entity_type', 'node')
          ->entityCondition('bundle', 'mycontenttype')
          ->propertyCondition('status', NODE_PUBLISHED)
          ->fieldCondition('field_mypdf', 'value', 'NULL', '!=')
          ->fieldCondition('field_mypdf', 'fid', 'NULL', '!=')
          ->fieldCondition('field_mypdf_image', 'fid', 'NULL', '!=')
    ?>

Originally, I was doing something along the lines of:

<?php 
$query = "SELECT node.nid AS nid, 'node' AS field_data_field_news_node_entity_type, 'node' AS field_data_field_news_date_text_node_entity_type, 'node' AS field_data_field_news_teaser_node_entity_type, 'node' AS field_data_field_news_body_node_entity_type, 'node' AS field_data_field_news_video_node_entity_type, 'node' AS field_data_field_news_video_image_node_entity_type, 'node' AS field_data_field_mypdf_node_entity_type
    FROM node
    INNER JOIN field_data_field_news_is_featured ON node.nid = field_data_field_news_is_featured.entity_id AND (field_data_field_news_is_featured.entity_type = 'node' AND field_data_field_news_is_featured.deleted = '0')";
?>

$result = db_query($query);

<?php foreach ($result as $row) : ?>

    <div class="featuredArticle">
        <?php print $row->field_mypdf_value; ?>
        <?php print $row->field_mypdf_fid; ?>
    </div>

<?php endforeach; ?>
  • what do you mean by featured field?? – Nitesh Sethia Jun 3 '15 at 20:22
  • One of the fields of the content type is a checkbox "featured" I only want to get the fields of the nodes (of my content type) with this field "featured" checked – srodrig Jun 3 '15 at 20:27
  • Can you not create a view adding featured as a 1 as one of the filter criteria..? – Nitesh Sethia Jun 3 '15 at 20:39
  • Yes, I can. The featured field is actual not the most important part. I just wanted to be sure of the best way to get the fields from content type – srodrig Jun 3 '15 at 20:57
  • Views module does this. – No Sssweat Jun 3 '15 at 22:15
1

I think choice is in between EntityQuery and db_select and EntityQuery seems to be a better choice for Entities as it takes care of joining different fields for you.

Reference Link: EntityFieldQuery vs Db_select() -

@Bart says: -

I think that the main reason prefering EntityFieldQuery over db_select is that you don't have to know about the lower-level structure, in other words: how stuff is stored in the database. This improves loose coupling.

@Berdir comment:-

This is correct, although it goes even further. The field storage is pluggable. The default implementation stores field data in a separate table per field in the database, but it can be replaced, there is for example an implementation which allows to store field data in MongoDB. If you want your code to be portable and not just work on your specific site configuration, you must use EntityFieldQuery.

  • This is helpful, thanks! I will need to loop through each row of the query ... I can do this wither either method? Also for the EntityFieldQuery, I can add additional fieldCondition? How would I do this just using a SELECT? – srodrig Jun 4 '15 at 12:29
  • Also, I need to query all nodes of a given content type, not just a single node. – srodrig Jun 4 '15 at 12:37
  • Please check the documentation at drupal.org/node/1343708, best would be to try it out. Your doubts will get clear. With no doubt, Entity Query is traversing through all your nodes. Yes you can add multiple fieldConditions. – arpitr Jun 4 '15 at 13:30
  • dropbucket.org/node/54 – arpitr Jun 4 '15 at 13:40
  • I updated original question with my EntityFieldQuery...what am I doing wrong? – srodrig Jun 4 '15 at 13:45

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.