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?

1 Answer 1


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');

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
    Apr 26, 2018 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, 2018 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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