For a specific entry in a specific section I would like to eager load all associated data from the related elements.

Say that I know the related elements are from the following field types:

  1. An assets field
  2. An entries field

And I know the field handles or IDs. This isn't supposed to be a "catch all" solution, it can be on a case by case basis.

I want to eagerly load all of these elements (including their relationships should they have some, i.e., the related entries have related images).

How would I go about this in a plugin?

I know I can write a hook or a custom controller action, I'm more curious about how I'd setup the calls themselves. I can't use Yii's with for Active Record because that isn't setup in the entry record itself.

Note, this is similar to the following question, but more direct: "Eager loading" of relations like Yii's "with" function

1 Answer 1


I wanted to do this too but none of the relationships are defined in the record classes.

An approach to achieving this would be to extend the Craft record classes in your own record classes and override the defineRelations method. This does however require that you understand CActiveRecord relations.

If you need any pointers give me a hollar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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