Answers.Body: <p>Once you've got your ECM, pass it off to the <a href="http://buildwithcraft.com/classreference/services/ElementsService#buildElementsQuery-detail"><code>buildElementsQuery</code></a> method...</p>
  
  <pre><code>$query = craft()-&gt;elements-&gt;buildElementsQuery($criteria);
  </code></pre>
  
  <p><strong>This will convert your ECM into a DbCommand object.</strong> From there, you can make further modifications to the query... like adding a <code>join</code> for your 3rd party table.</p>
  
  <pre><code>$query-&gt;join('myplugintable myplugintable', 'myplugintable.id = elements.id');
  $query-&gt;order('myplugintable.mysortcolumn');
  
  return $query-&gt;queryAll();
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-26T18:20:17.103Z
Answers.Text: 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?
Answers.UserDisplayName: Franz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-27T22:01:38.323Z
Answers.Text: @FranzThüs I'd recommend posting that as [a new question](https://craftcms.stackexchange.com/questions/ask). Feel free to link back to this thread.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-27T20:50:55.870Z
Answers.LastActivityDate: 2014-09-27T20:50:55.870Z
Answers.UserDisplayName: Lindsey D
Body: <p>I need to join my <strong>3rd party table</strong> with a standard <strong>ElementCriteriaModel (ECM)</strong>. 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.</p>
  
  <p>By default, an ECM generates a query which joins multiple native tables. According to Brad in <a href="https://craftcms.stackexchange.com/a/2776/45">this answer...</a></p>
  
  <blockquote>
    <p>... think of ElementCriteriaModel as an abstraction on top of DbCommand that makes it easy to search for Elements.</p>
  </blockquote>
  
  <p>My plugin does not have either a custom Field Type or custom Element Type (which means I can't use <code>modifyElementsQuery</code> when retrieving the data).</p>
  
  <p><strong>How can I expand my ECM query to take additional SQL commands into account?</strong></p>
  
Tags: <plugin-development><elementcriteriamodel><queries>
Title: How can I do more complicated queries with an ElementCriteriaModel?
ThreadText: # How can I do more complicated queries with an ElementCriteriaModel?
  
  (Asked by: username_0 on 2014-09-27)
  
  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...](https://craftcms.stackexchange.com/a/2776/45)
  
  > ... 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?**
  
  **Tags:** plugin-development, elementcriteriamodel, queries
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2014-09-27. Score: 10 | Accepted answer
  
  Once you've got your ECM, pass it off to the [`buildElementsQuery`](http://buildwithcraft.com/classreference/services/ElementsService#buildElementsQuery-detail) 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();
  
  ```
  
  > **Comments:**
  > **username_1**: 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?  
  > **username_0**: @FranzThüs I'd recommend posting that as [a new question](https://craftcms.stackexchange.com/questions/ask). Feel free to link back to this thread.  
  
  ---
  
  