7

I need to join my 3rd party table with a standard ElementCriteriaModel (ECM). I'd like to start with an ordinary ECM, so my plugin will be compatible with any element type which may exist, now or in the future.

By default, an ECM generates a query which joins multiple native tables. According to Brad in this answer...

... think of ElementCriteriaModel as an abstraction on top of DbCommand that makes it easy to search for Elements.

My plugin does not have either a custom Field Type or custom Element Type (which means I can't use modifyElementsQuery when retrieving the data).

How can I expand my ECM query to take additional SQL commands into account?

10

Once you've got your ECM, pass it off to the buildElementsQuery method...

$query = craft()->elements->buildElementsQuery($criteria);

This will convert your ECM into a DbCommand object. From there, you can make further modifications to the query... like adding a join for your 3rd party table.

$query->join('myplugintable myplugintable', 'myplugintable.id = elements.id');
$query->order('myplugintable.mysortcolumn');

return $query->queryAll();
  • Any idea how I would go about using Craft's pagination in this scenario? And is there a way to get the total result count for the query when using a LIMIT clause on the initial ECM? – Franz Thüs Apr 26 '18 at 18:20
  • @FranzThüs I'd recommend posting that as a new question. Feel free to link back to this thread. – Lindsey D Apr 27 '18 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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