<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>

CC BY-SA 4.0
2019-02-12T15:58:43.730Z
2019-02-12T15:58:43.730Z
Torbjørn P. Larssen
<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>

<assets><elementcriteriamodel><eager-loading><element>
Eager Loading Image Transforms In Plugin
# Eager Loading Image Transforms In Plugin

(Asked by: username_0 on 2017-04-06)

The craft docs on eager loading 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"
];


---

