<p>This is called <a href="https://craftcms.com/docs/templating/eager-loading-elements" rel="nofollow noreferrer">Eager Loading</a> you have to do </p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;order('postDate asc');
$criteria-&gt;dateUpdated = array('and', '&gt;=' . $this-&gt;dateFrom-&gt;format(DateTime::MYSQL_DATETIME), '&lt;=' . $this-&gt;dateTo-&gt;format(DateTime::MYSQL_DATETIME));
$criteria-&gt;limit('null');

// ========== start change ===============
$criteria-&gt;with = ['video'];
// ========== end change  ================

$responses = $criteria-&gt;find();
</code></pre>

<p><strong>Edit</strong>: One of the most important things when working with Craft.</p>

<blockquote>
  <p>I can see that $response->video is an array containing the sourceElement id and field id.</p>
</blockquote>

<p>No, no and no. Many people believe this, or many other things, but that is not true. Your <code>$response-&gt;video</code> will contain an object of type <code>ElementCriteriaModel</code>. <code>$respose-&gt;video</code> is exactly the same type of object like <code>$criteria = craft()-&gt;elements-&gt;getCriteria();</code></p>

<p>Thus you can do </p>

<pre><code>$videos = $response-&gt;video-&gt;order('')-&gt;limit(2)-&gt;find()
</code></pre>

CC BY-SA 3.0
2018-03-06T11:15:57.417Z
The fact that $responses-video was actually also an ElementCriteriaModel just came back to me and I've solved my problem by remembering that! But Eager Loading sounds like exactly what I need. Thanks.
foamcow
CC BY-SA 3.0
2018-03-06T10:52:08.670Z
2018-03-06T11:05:39.783Z
2018-03-06T11:05:39.783Z
Robin Schambach
<p>I am working on a custom plugin which gets some entry data using</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;order('postDate asc');
$criteria-&gt;dateUpdated = array('and', '&gt;=' . $this-&gt;dateFrom-&gt;format(DateTime::MYSQL_DATETIME), '&lt;=' . $this-&gt;dateTo-&gt;format(DateTime::MYSQL_DATETIME));
$criteria-&gt;limit('null');

$responses = $criteria-&gt;find();
</code></pre>

<p>The returned entries contain a relationship to entries in another channel. In this case, the relationship field is named 'video'. So when looping through <code>$responses</code> I can see that <code>$response-&gt;video</code> is an array containing the sourceElement id and field id.</p>

<p>Is it possible to get the complete entry data for the related entry returned in the same query rather than needing to perform an additional query?</p>

<elementcriteriamodel><eager-loading><element>
How to get related entry data using ElementCriteriaModel in a plugin
# How to get related entry data using ElementCriteriaModel in a plugin

(Asked by: username_0 on 2018-03-06)

I am working on a custom plugin which gets some entry data using
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->order('postDate asc');
$criteria->dateUpdated = array('and', '>=' . $this->dateFrom->format(DateTime::MYSQL_DATETIME), '<=' . $this->dateTo->format(DateTime::MYSQL_DATETIME));
$criteria->limit('null');

$responses = $criteria->find();

The returned entries contain a relationship to entries in another channel. In this case, the relationship field is named 'video'. So when looping through $responses I can see that $response->video is an array containing the sourceElement id and field id.
Is it possible to get the complete entry data for the related entry returned in the same query rather than needing to perform an additional query?

**Tags:** elementcriteriamodel, eager-loading, element


---

## 1 answers

### Answer by username_1 on 2018-03-06. Score: 2 | Accepted answer

This is called Eager Loading you have to do 
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->order('postDate asc');
$criteria->dateUpdated = array('and', '>=' . $this->dateFrom->format(DateTime::MYSQL_DATETIME), '<=' . $this->dateTo->format(DateTime::MYSQL_DATETIME));
$criteria->limit('null');

// ========== start change ===============
$criteria->with = ['video'];
// ========== end change  ================

$responses = $criteria->find();

Edit: One of the most important things when working with Craft.

I can see that $response->video is an array containing the sourceElement id and field id.

No, no and no. Many people believe this, or many other things, but that is not true. Your $response->video will contain an object of type ElementCriteriaModel. $respose->video is exactly the same type of object like $criteria = craft()->elements->getCriteria();
Thus you can do 
$videos = $response->video->order('')->limit(2)->find()

> **Comments:**
> **username_0**: The fact that $responses-video was actually also an ElementCriteriaModel just came back to me and I've solved my problem by remembering that! But Eager Loading sounds like exactly what I need. Thanks.  

---

