<p>The article on StraightUpCraft should be able to point you in the right direction. PHP stuff is towards the bottom of the article.</p>

<p><a href="https://straightupcraft.com/articles/examples-of-eager-loading-elements-in-twig-and-php" rel="nofollow">Examples of Eager Loading Elements in Twig and PHP</a></p>

<p>Also this Craft hangout is worth watching, it gives a lot of detail on Eager loading. It's over an hour long though, so set yourself some time aside to watch it.</p>

<p><a href="https://straightupcraft.com/events/eager-loading-elements" rel="nofollow">Eager loading Elements</a></p>

CC BY-SA 3.0
2016-03-26T11:02:55.750Z
Hey Steve, thanks so much for the response. I gave this a try but still can't seem to get it to work. I tried using **$criteria->with = array('Entry');** as well as a few variations. I'll watch through the whole podcast soon and let you know if I find my answer in there.
Michael Witwicki
CC BY-SA 3.0
2016-03-26T14:04:54.150Z
I haven't tried eager loading myself yet, but from what I gleaned from the hangout is you eager load individual fields rather than the whole thing. So you need put the handle of the actual field (so your Uri field I'd guess), rather than just 'Entry'.
Steve Holland
CC BY-SA 3.0
2016-03-27T23:21:45.513Z
Yeah, the `with` parameter expects a field handle for eager loading the related element models. If I understand correctly, the OP doesn't use a relationship field to relate the elements and that parameter won't work in this case.
carlcs
CC BY-SA 3.0
2016-03-28T12:51:57.597Z
@carlcs you are totally right. I think I just had too many new concepts running around in my head at the same time trying to get familiar with Craft/Yii. I was able to accomplish what I wanted by simply joining in the elements_i18n table in modifylementsQuery. Thanks so much everyone.
Michael Witwicki
CC BY-SA 3.0
2016-03-26T09:26:29.260Z
2016-03-26T09:36:44.393Z
2016-03-26T09:36:44.393Z
Steve Holland
<p>trying to get my head wrapped around eager loading element relationships in my plugin. A little context, I’m working on a navigation module and I have a Node element type, which belongs to a Navigation and has one Entry element type:</p>

<pre><code>public function defineRelations(){
return array(
  'element'    =&gt; array(static::BELONGS_TO, 'ElementRecord', 'id', 'required' =&gt; true, 'onDelete' =&gt; static::CASCADE),
  'navigation' =&gt; array(static::BELONGS_TO, 'Navee_NavigationRecord', 'required' =&gt; true, 'onDelete' =&gt; static::CASCADE),
  'entry'      =&gt; array(static::HAS_ONE, 'EntryRecord', 'entryId', 'required' =&gt; false),
);
</code></pre>

<p>}</p>

<p>When I’m getting my nodes, I’d like to eager load the entry for each node. This is where I’m just sort of falling down on they syntax. If I currently have (without eager loading):</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria('Navee_Node');
</code></pre>

<p>Would I eager load the entries by doing something like:</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria('Navee_Node')-&gt;with(ElementType::Entry);
</code></pre>

<p>I should note that what I'm really trying to do is pull the uri for each related entry without having to run an additional query every time I iterate over the navigation. So maybe my thinking is wrong and I don't need to eager load the entire EntryElement type, but just a particular field?</p>

<p>Thanks so much in advance for any insight you can provide.</p>

<p>MW</p>

<plugin-development><plugins><elementcriteriamodel><elementtype><eager-loading>
Eager loading EntryElementType from custom ElementType in a plugin
# Eager loading EntryElementType from custom ElementType in a plugin

(Asked by: username_0 on 2016-03-25)

trying to get my head wrapped around eager loading element relationships in my plugin. A little context, I’m working on a navigation module and I have a Node element type, which belongs to a Navigation and has one Entry element type:
public function defineRelations(){
return array(
  'element'    => array(static::BELONGS_TO, 'ElementRecord', 'id', 'required' => true, 'onDelete' => static::CASCADE),
  'navigation' => array(static::BELONGS_TO, 'Navee_NavigationRecord', 'required' => true, 'onDelete' => static::CASCADE),
  'entry'      => array(static::HAS_ONE, 'EntryRecord', 'entryId', 'required' => false),
);

}
When I’m getting my nodes, I’d like to eager load the entry for each node. This is where I’m just sort of falling down on they syntax. If I currently have (without eager loading):
$criteria = craft()->elements->getCriteria('Navee_Node');

Would I eager load the entries by doing something like:
$criteria = craft()->elements->getCriteria('Navee_Node')->with(ElementType::Entry);

I should note that what I'm really trying to do is pull the uri for each related entry without having to run an additional query every time I iterate over the navigation. So maybe my thinking is wrong and I don't need to eager load the entire EntryElement type, but just a particular field?
Thanks so much in advance for any insight you can provide.
MW

**Tags:** plugin-development, plugins, elementcriteriamodel, elementtype, eager-loading

---

## 1 answers

### Answer by username_1 on 2016-03-26. Score: 2 | Accepted answer

The article on StraightUpCraft should be able to point you in the right direction. PHP stuff is towards the bottom of the article.

[Examples of Eager Loading Elements in Twig and PHP](https://straightupcraft.com/articles/examples-of-eager-loading-elements-in-twig-and-php)

Also this Craft hangout is worth watching, it gives a lot of detail on Eager loading. It's over an hour long though, so set yourself some time aside to watch it.

[Eager loading Elements](https://straightupcraft.com/events/eager-loading-elements)

> **Comments:**
> **username_0**: Hey Steve, thanks so much for the response. I gave this a try but still can't seem to get it to work. I tried using **$criteria->with = array('Entry');** as well as a few variations. I'll watch through the whole podcast soon and let you know if I find my answer in there.  
> **username_1**: I haven't tried eager loading myself yet, but from what I gleaned from the hangout is you eager load individual fields rather than the whole thing. So you need put the handle of the actual field (so your Uri field I'd guess), rather than just 'Entry'.  
> **username_2**: Yeah, the `with` parameter expects a field handle for eager loading the related element models. If I understand correctly, the OP doesn't use a relationship field to relate the elements and that parameter won't work in this case.  
> **username_0**: @carlcs you are totally right. I think I just had too many new concepts running around in my head at the same time trying to get familiar with Craft/Yii. I was able to accomplish what I wanted by simply joining in the elements_i18n table in modifylementsQuery. Thanks so much everyone.  

---

