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!
withcriteria. – user6848 Aug 01 '17 at 11:47