1

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

2

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

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

  • 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 Mar 26 '16 at 11:02
  • 2
    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 Mar 26 '16 at 14:04
  • 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 Mar 27 '16 at 23:21
  • @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 Mar 28 '16 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.