Answers.Body: <p>I fixed this now by using the method buildElementsQuery, like so:</p>
  
  <pre><code>    $query = craft()-&gt;elements-&gt;buildElementsQuery($criteria, $contentTable, $fieldColumns);
  </code></pre>
  
  <p>After that, I was able to add custom joins to the query:</p>
  
  <pre><code>$query
      -&gt;addSelect('sport.title sportTitle')
      -&gt;leftJoin('relations sportRelation', 'sportRelation.sourceId = elements.id AND sportRelation.fieldId = 71')
      -&gt;leftJoin('content sport', 'sport.elementId = sportRelation.targetId')
  </code></pre>
  
  <p>This gave me access to the data I need, without running extra queries. Thanks go out to Bob Olde Hampsink for showing me this.</p>
  
  <p>I'd love to hear though if there is a simpler way that won't require custom SQL, but for now this will do the trick.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-02T11:53:24.563Z
Answers.LastActivityDate: 2015-04-02T11:53:24.563Z
Answers.UserDisplayName: 24creative
Body: <p>I have the following code:</p>
  
  <pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
  $criteria-&gt;section = 'submissions';
  
  // Loop
  foreach ($criteria as $entry)
  {
      echo $entry-&gt;id.'&lt;br /&gt;';
      echo $entry-&gt;title.'&lt;br /&gt;';
      echo $entry-&gt;sport.'&lt;br /&gt;';
  }
  </code></pre>
  
  <p>The <strong>sport</strong> field is an Entry field, pointing to an entry in a different channel. My question is, how can I get the data from the related entry (i.e. the title) without an extra query per row? Can I eager load it? Or get the sport entries in a separate query and then combine the data?</p>
  
  <p>I need to get the title of the related sport entry. The following works but gives an extra query for every row:</p>
  
  <pre><code>$entry-&gt;sport-&gt;first()['title'];
  </code></pre>
  
  <p>Anyone? Thanks in advance</p>
  
Tags: <plugin-development><database><elementcriteriamodel>
Title: Get related entry data within a plugin (ElementCriteriaModel) without extra queries
ThreadText: # Get related entry data within a plugin (ElementCriteriaModel) without extra queries
  
  (Asked by: username_0 on 2015-04-02)
  
  I have the following code:
  
  ```
  $criteria = craft()->elements->getCriteria(ElementType::Entry);
  $criteria->section = 'submissions';
  
  // Loop
  foreach ($criteria as $entry)
  {
      echo $entry->id.'<br />';
      echo $entry->title.'<br />';
      echo $entry->sport.'<br />';
  }
  
  ```
  
  The **sport** field is an Entry field, pointing to an entry in a different channel. My question is, how can I get the data from the related entry (i.e. the title) without an extra query per row? Can I eager load it? Or get the sport entries in a separate query and then combine the data?
  
  I need to get the title of the related sport entry. The following works but gives an extra query for every row:
  
  ```
  $entry->sport->first()['title'];
  
  ```
  
  Anyone? Thanks in advance
  
  **Tags:** plugin-development, database, elementcriteriamodel
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2015-04-02. Score: 5
  
  I fixed this now by using the method buildElementsQuery, like so:
  
  ```
      $query = craft()->elements->buildElementsQuery($criteria, $contentTable, $fieldColumns);
  
  ```
  
  After that, I was able to add custom joins to the query:
  
  ```
  $query
      ->addSelect('sport.title sportTitle')
      ->leftJoin('relations sportRelation', 'sportRelation.sourceId = elements.id AND sportRelation.fieldId = 71')
      ->leftJoin('content sport', 'sport.elementId = sportRelation.targetId')
  
  ```
  
  This gave me access to the data I need, without running extra queries. Thanks go out to Bob Olde Hampsink for showing me this.
  
  I'd love to hear though if there is a simpler way that won't require custom SQL, but for now this will do the trick.
  
  
  ---
  
  