Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @return mixed
- */
- public function getAllEntries(){
- // I want to create the code with the best possible performance, so I would
- // like to avoid using $field = craft()->fields()->getFieldByHandle();
- // and instead inserting the id directly, my ids are 13 and 14
- $categoryFieldId = 13;
- $typeFieldId = 14;
- // insert your local here
- $locale = 'de';
- // insert your section handle, for my test system it is news
- $sectionHandle = 'news';
- $dbCommand = craft()->db->createCommand();
- // select all your needed fields, I don't want to loop through your field layout since
- // that would result in multiple queries and I want to have the best possible performance
- $dbCommand->select([
- // category is your category you can check all it's fields in the craft_contents table
- 'category.title as categoryTitle',
- // type is your related type, all columns are in craft_contents as well
- 'type.title as typeTitle',
- // content is your entry
- 'content.title as elementTitle',
- 'content.field_body as elementText'
- ]);
- $dbCommand->from('elements as elements');
- $dbCommand->join('elements_i18n as elements_i18n', 'elements_i18n.elementId = elements.id');
- $dbCommand->join('content as content', 'content.elementId = elements.id');
- $dbCommand->join('entries as entries', 'entries.id = elements.id');
- $dbCommand->join('sections as sections', 'sections.id = entries.sectionId');
- $dbCommand->leftJoin('structures as structures', 'structures.id = sections.structureId');
- $dbCommand->leftJoin('structureelements as structureelements', '(structureelements.structureId = structures.id) AND (structureelements.elementId = entries.id)');
- $dbCommand->setGroup('elements.id');
- $dbCommand->andWhere(array(
- 'sections.handle' => $sectionHandle,
- 'elements_i18n.locale' => $locale,
- 'content.locale' => $locale,
- 'elements.enabled' => 1,
- 'elements_i18n.enabled' => 1,
- // you can check here for entries.expiryDate and these things here as well
- ));
- $dbCommand->order('postDate DESC');
- $dbCommand->setGroup('elements.id');
- // create the relations, because you named the fields type and country and not types and countries
- // I assume you link only one country/type with an entry otherwise we need to change the query here,
- // let me know if that's the case
- $dbCommand->join("relations typeRelation", "typeRelation.sourceId = elements.id");
- $dbCommand->join("relations categoryRelation", "categoryRelation.sourceId = elements.id");
- $dbCommand->andWhere(array(
- 'categoryRelation.fieldId' => $categoryFieldId
- ));
- $dbCommand->andWhere(array(
- 'typeRelation.fieldId' => $typeFieldId
- ));
- $dbCommand->join("content as category", "category.elementId = categoryRelation.targetId");
- $dbCommand->join("content as type", "type.elementId = typeRelation.targetId");
- $elements = $dbCommand->queryAll();
- return $elements;
- }
- {% set entries = craft.yourPluginHandle.getAllEntries() %}
- {% for entry in entries %}
- <pre>
- {{ dump(entry) }}
- </pre>
- {% endfor %}
Add Comment
Please, Sign In to add comment