Guest User

Untitled

a guest
Nov 15th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. /**
  2. * @return mixed
  3. */
  4. public function getAllEntries(){
  5. // I want to create the code with the best possible performance, so I would
  6. // like to avoid using $field = craft()->fields()->getFieldByHandle();
  7. // and instead inserting the id directly, my ids are 13 and 14
  8. $categoryFieldId = 13;
  9. $typeFieldId = 14;
  10. // insert your local here
  11. $locale = 'de';
  12. // insert your section handle, for my test system it is news
  13. $sectionHandle = 'news';
  14.  
  15. $dbCommand = craft()->db->createCommand();
  16. // select all your needed fields, I don't want to loop through your field layout since
  17. // that would result in multiple queries and I want to have the best possible performance
  18. $dbCommand->select([
  19. // category is your category you can check all it's fields in the craft_contents table
  20. 'category.title as categoryTitle',
  21. // type is your related type, all columns are in craft_contents as well
  22. 'type.title as typeTitle',
  23. // content is your entry
  24. 'content.title as elementTitle',
  25. 'content.field_body as elementText'
  26. ]);
  27. $dbCommand->from('elements as elements');
  28. $dbCommand->join('elements_i18n as elements_i18n', 'elements_i18n.elementId = elements.id');
  29. $dbCommand->join('content as content', 'content.elementId = elements.id');
  30. $dbCommand->join('entries as entries', 'entries.id = elements.id');
  31. $dbCommand->join('sections as sections', 'sections.id = entries.sectionId');
  32. $dbCommand->leftJoin('structures as structures', 'structures.id = sections.structureId');
  33. $dbCommand->leftJoin('structureelements as structureelements', '(structureelements.structureId = structures.id) AND (structureelements.elementId = entries.id)');
  34. $dbCommand->setGroup('elements.id');
  35.  
  36. $dbCommand->andWhere(array(
  37. 'sections.handle' => $sectionHandle,
  38. 'elements_i18n.locale' => $locale,
  39. 'content.locale' => $locale,
  40. 'elements.enabled' => 1,
  41. 'elements_i18n.enabled' => 1,
  42. // you can check here for entries.expiryDate and these things here as well
  43. ));
  44. $dbCommand->order('postDate DESC');
  45. $dbCommand->setGroup('elements.id');
  46.  
  47. // create the relations, because you named the fields type and country and not types and countries
  48. // I assume you link only one country/type with an entry otherwise we need to change the query here,
  49. // let me know if that's the case
  50. $dbCommand->join("relations typeRelation", "typeRelation.sourceId = elements.id");
  51. $dbCommand->join("relations categoryRelation", "categoryRelation.sourceId = elements.id");
  52. $dbCommand->andWhere(array(
  53. 'categoryRelation.fieldId' => $categoryFieldId
  54. ));
  55. $dbCommand->andWhere(array(
  56. 'typeRelation.fieldId' => $typeFieldId
  57. ));
  58. $dbCommand->join("content as category", "category.elementId = categoryRelation.targetId");
  59. $dbCommand->join("content as type", "type.elementId = typeRelation.targetId");
  60.  
  61.  
  62. $elements = $dbCommand->queryAll();
  63. return $elements;
  64. }
  65.  
  66. {% set entries = craft.yourPluginHandle.getAllEntries() %}
  67. {% for entry in entries %}
  68. <pre>
  69. {{ dump(entry) }}
  70. </pre>
  71. {% endfor %}
Add Comment
Please, Sign In to add comment