Answers.Body: <p>Eager loading only works when your field is a <code>RelationField</code> it does not work with <code>Text</code> fields. If you goal is to reduce the length of the queries and to maximize your performance you could fetch the raw rows instead of populating the elements.</p>
  
  <pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
  $criteria-&gt;status = null;
  $criteria-&gt;limit = null;
  $criteria-&gt;group = 'productBrand';
  
  // create a query
  $query = craft()-&gt;elements-&gt;buildElementsQuery($criteria);
  // select only the rows you want/need
  $query-&gt;select('elements.id as id, content.field_posBrandKey as posBrandKey');
  $brands = [];
  // execute it
  $rows = $query-&gt;queryAll();
  
  foreach($rows as $row){
      $brands[$row['posBrandKey']] = $row['id'];
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-09T11:29:33.493Z
Answers.LastActivityDate: 2018-05-09T11:29:33.493Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I'm trying to get some data with eager loading, which results in more queries than before.</p>
  
  <p>Is there another way to tune down the number of queries as low as possible? As you can see i only need the posBrandKey (text) and id values.</p>
  
  <pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
  $criteria-&gt;status = null;
  $criteria-&gt;limit = null;
  $criteria-&gt;group = 'productBrand';
  $criteria-&gt;with = array('posBrandKey');
  
  $brands = array();
  foreach ($criteria as $entry) {
      $key = $entry-&gt;posBrandKey;
      $brands[$key] = $entry-&gt;id;
  }
  </code></pre>
  
  <p>My goal is to have the best performance possible, this is one of the many queries I call in the same script.</p>
  
Tags: <plugin-development><performance><queries><eager-loading>
Title: Correctly using eager loading in Plugin development
ThreadText: # Correctly using eager loading in Plugin development
  
  (Asked by: username_0 on 2018-05-09)
  
  I'm trying to get some data with eager loading, which results in more queries than before.
  
  Is there another way to tune down the number of queries as low as possible? As you can see i only need the posBrandKey (text) and id values.
  
  ```
  $criteria = craft()->elements->getCriteria(ElementType::Category);
  $criteria->status = null;
  $criteria->limit = null;
  $criteria->group = 'productBrand';
  $criteria->with = array('posBrandKey');
  
  $brands = array();
  foreach ($criteria as $entry) {
      $key = $entry->posBrandKey;
      $brands[$key] = $entry->id;
  }
  
  ```
  
  My goal is to have the best performance possible, this is one of the many queries I call in the same script.
  
  **Tags:** plugin-development, performance, queries, eager-loading
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-05-09. Score: 3 | Accepted answer
  
  Eager loading only works when your field is a `RelationField` it does not work with `Text` fields. If you goal is to reduce the length of the queries and to maximize your performance you could fetch the raw rows instead of populating the elements.
  
  ```
  $criteria = craft()->elements->getCriteria(ElementType::Category);
  $criteria->status = null;
  $criteria->limit = null;
  $criteria->group = 'productBrand';
  
  // create a query
  $query = craft()->elements->buildElementsQuery($criteria);
  // select only the rows you want/need
  $query->select('elements.id as id, content.field_posBrandKey as posBrandKey');
  $brands = [];
  // execute it
  $rows = $query->queryAll();
  
  foreach($rows as $row){
      $brands[$row['posBrandKey']] = $row['id'];
  }
  
  ```
  
  
  ---
  
  