Answers.Body: <p>I came across this post while searching for the same answer. Figured it out eventually. For future reference, the correct syntax in a (Craft 2) plugin would be:</p>
  
  <pre><code>$criterea-&gt;with = [
      ["coolImage", ["withTransforms" =&gt; ["transform"]]],
      "otherAssetField",
      "someCategoryField"
  ];
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-12T15:58:43.730Z
Answers.LastActivityDate: 2019-02-12T15:58:43.730Z
Answers.UserDisplayName: Torbjørn P. Larssen
Body: <p>The <a href="https://craftcms.com/docs/templating/eager-loading-elements" rel="nofollow noreferrer">craft docs on eager loading</a> show you how to eager load image transforms in twig. </p>
  
  <pre><code>{% set entries = craft.entries({
     with: [
          ['coolImage', { withTransforms: ['transform'] }]
      ]
  }) %}
  </code></pre>
  
  <p>However I can't figure out the syntax for achieving the same functionality within a plugin using the ElementCritereaModel.</p>
  
  <p>I've made various attempts but they've all resulted in HUGE EXPLOSIONS of the php error variety.</p>
  
  <pre><code>$criterea-&gt;with = [
      "coolImage" =&gt; [
          "withTransforms" =&gt; ["transform"]
      ],
      "otherAssetField",
      "someCategoryField"
  ];
  </code></pre>
  
  <p>Of course above wouldn't work because you're switching between field slugs being keys/values in the array.</p>
  
  <p>Using a similar syntax to the docs in twig would be:</p>
  
  <pre><code>$criterea-&gt;with = [
      "teaserImage",
      ["withTransforms" =&gt; ["peopleListing"]],
      "peoplePffice",
      "peopleCategories"
  ];
  </code></pre>
  
  <p>But that also results in explosions.</p>
  
  <p>Help!!! (thanks)</p>
  
Tags: <assets><elementcriteriamodel><eager-loading><element>
Title: Eager Loading Image Transforms In Plugin
ThreadText: # Eager Loading Image Transforms In Plugin
  
  (Asked by: username_0 on 2017-04-06)
  
  The [craft docs on eager loading](https://craftcms.com/docs/templating/eager-loading-elements) show you how to eager load image transforms in twig.
  
  ```
  {% set entries = craft.entries({
     with: [
          ['coolImage', { withTransforms: ['transform'] }]
      ]
  }) %}
  
  ```
  
  However I can't figure out the syntax for achieving the same functionality within a plugin using the ElementCritereaModel.
  
  I've made various attempts but they've all resulted in HUGE EXPLOSIONS of the php error variety.
  
  ```
  $criterea->with = [
      "coolImage" => [
          "withTransforms" => ["transform"]
      ],
      "otherAssetField",
      "someCategoryField"
  ];
  
  ```
  
  Of course above wouldn't work because you're switching between field slugs being keys/values in the array.
  
  Using a similar syntax to the docs in twig would be:
  
  ```
  $criterea->with = [
      "teaserImage",
      ["withTransforms" => ["peopleListing"]],
      "peoplePffice",
      "peopleCategories"
  ];
  
  ```
  
  But that also results in explosions.
  
  Help!!! (thanks)
  
  **Tags:** assets, elementcriteriamodel, eager-loading, element
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-02-12. Score: 1
  
  I came across this post while searching for the same answer. Figured it out eventually. For future reference, the correct syntax in a (Craft 2) plugin would be:
  
  ```
  $criterea->with = [
      ["coolImage", ["withTransforms" => ["transform"]]],
      "otherAssetField",
      "someCategoryField"
  ];
  
  ```
  
  
  ---
  
  