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

<p>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.</p>

<p>If you need any pointers give me a hollar.</p>

CC BY-SA 3.0
2015-05-03T11:35:25.200Z
2015-05-03T11:35:25.200Z
Jay
<p>For a specific entry in a specific section I would like to eager load all associated data from the related elements.</p>

<p>Say that I know the related elements are from the following field types:</p>

<ol>
<li>An assets field</li>
<li>An entries field</li>
</ol>

<p>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.</p>

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

<p>How would I go about this in a plugin?</p>

<p>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 <code>with</code> for Active Record because that isn't setup in the entry record itself.</p>

<p><strong>Note</strong>, this is similar to the following question, but more direct:
<a href="https://craftcms.stackexchange.com/questions/4176/eager-loading-of-relations-like-yiis-with-function">&quot;Eager loading&quot; of relations like Yii&#39;s &quot;with&quot; function</a></p>

<plugin-development><relations>
Is there a good way to eager load Craft's Element Relations through a plugin?
# Is there a good way to eager load Craft's Element Relations through a plugin?

(Asked by: username_0 on 2014-12-16)

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](https://craftcms.stackexchange.com/questions/4176/eager-loading-of-relations-like-yiis-with-function)

**Tags:** plugin-development, relations


---

## 1 answers

### Answer by username_1 on 2015-05-03. Score: 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.


---

