<p>I need to tune performance on a Craft 2 build. We are quite new to Craft CMS, so please bear with me if the question sounds silly.</p>

<p>We need to render a page that needs to display information from 4 sections, A->B->C->D, with the latter having a reference to the former, i.e. B has a field linking to A, C has a field linking to B, and D has a field linking to C. The page needs to list A entries with information gathered from B, C, D. We ended up using <code>relatedTo</code> in three foreach lookup until D, which turned out to be super inefficient. I managed to find out that using eager-loading of elements can greatly reduce the number of queries, which in my case, I've got 400+ queries down to about 40. The page load time is roughly 2s (from previous 30+ seconds!).</p>

<p>However, I think there might be an even better way to query the db. Currently, I am querying all elements of A, taking this array to query all related entries of B, which again is taken to query all entries of C. On querying entries of C, this is where eager-loading happens. The query on C section looks like this:</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = 'Section A';
$criteria-&gt;with = array(
    'b',
    'b.field_a',
    'd',
);
$criteria-&gt;relatedTo = array(
    'targetElement' =&gt; $obj,
    'field' =&gt; 'field_d'
);
</code></pre>

<p>I am wondering if there's a way to query related entries based on a field from an ancestor, like</p>

<pre><code>$criteria-&gt;relatedTo = array(
    'targetElement' =&gt; $obj,
    'field' =&gt; 'field_d.c.b.a.field'
);
</code></pre>

<p>That would save me from querying B and C entries.</p>

<p>Many thanks!</p>

CC BY-SA 3.0
2017-08-01T10:45:59.290Z
It sounds like D is a child of C which is a child of B which is a child of A. Is it possible to use a structure for this or do all of these sections need to be on the same level?
Cavell Blood
CC BY-SA 3.0
2017-08-01T11:47:03.453Z
I am not sure about structures to be honest. As far as I can see, the main difference between channels and structures is the ordering ability. In my case, it's simply that multiple types of entries chain one another. Anyway, I have worked around it by changing the relationship from manyToOne to oneToMany, i.e, put the relation field on the "one" side, so I can use `with` criteria.
user6848
CC BY-SA 3.0
2017-08-01T15:46:03.963Z
Glad you were able to figure it out! Post the code here as the answer if you get a chance.
Cavell Blood
<craft-features>
Entry reverse lookup on ancestor's field
# Entry reverse lookup on ancestor's field

(Asked by: username_0 on 2017-07-26)

I need to tune performance on a Craft 2 build. We are quite new to Craft CMS, so please bear with me if the question sounds silly.
We need to render a page that needs to display information from 4 sections, A->B->C->D, with the latter having a reference to the former, i.e. B has a field linking to A, C has a field linking to B, and D has a field linking to C. The page needs to list A entries with information gathered from B, C, D. We ended up using relatedTo in three foreach lookup until D, which turned out to be super inefficient. I managed to find out that using eager-loading of elements can greatly reduce the number of queries, which in my case, I've got 400+ queries down to about 40. The page load time is roughly 2s (from previous 30+ seconds!).
However, I think there might be an even better way to query the db. Currently, I am querying all elements of A, taking this array to query all related entries of B, which again is taken to query all entries of C. On querying entries of C, this is where eager-loading happens. The query on C section looks like this:
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'Section A';
$criteria->with = array(
    'b',
    'b.field_a',
    'd',
);
$criteria->relatedTo = array(
    'targetElement' => $obj,
    'field' => 'field_d'
);

I am wondering if there's a way to query related entries based on a field from an ancestor, like
$criteria->relatedTo = array(
    'targetElement' => $obj,
    'field' => 'field_d.c.b.a.field'
);

That would save me from querying B and C entries.
Many thanks!

**Tags:** craft-features

> **Comments:**
> **username_1**: It sounds like D is a child of C which is a child of B which is a child of A. Is it possible to use a structure for this or do all of these sections need to be on the same level?  
> **username_0**: I am not sure about structures to be honest. As far as I can see, the main difference between channels and structures is the ordering ability. In my case, it's simply that multiple types of entries chain one another. Anyway, I have worked around it by changing the relationship from manyToOne to oneToMany, i.e, put the relation field on the "one" side, so I can use `with` criteria.  
> **username_1**: Glad you were able to figure it out! Post the code here as the answer if you get a chance.  

---

## 0 answers

