<p>I found out I was not able to accomplish what I needed by using <strong>modifyElementsQuery</strong>.  So I created my own <strong>ElementCriteriaModel</strong> &amp; <strong>buildElementsQuery</strong> for my plugin. This allows the criteria to be built from my table directly instead of elements table.  </p>

CC BY-SA 3.0
2015-08-13T07:07:01.390Z
Any way you could share the code you used to do this?
Jarrett Barnett
CC BY-SA 3.0
2015-08-20T16:48:48.400Z
@JarrettBarnett I followed Crafts internal ElementCriteriaModel & ElementsService.   The customization came in with the BuildElementsQuery method you find in ElementsService.  View comments here: http://craftcms.stackexchange.com/questions/5134/what-is-the-proper-way-to-fetch-items-from-a-custom-db-table-and-output-them-in/5201#comment7467_5201 There is a link to a plugin that helped me move in the right direction.
a-am
CC BY-SA 3.0
2015-01-14T21:17:49.690Z
2015-01-15T06:18:45.603Z
2015-01-15T06:18:45.603Z
a-am
<p>I need to join my <strong>plugin's custom table</strong> to a an <strong>ElementCriteriaModel (ECM)</strong>. I am trying to use <strong>modifyElementsQuery</strong>. </p>

<p>In the plugin's custom table, rows can share an element/entry via a separate id.</p>

<p><strong>Can I achieve this by adding to the modifyElementsQuery? What would the query be to accomplish this?</strong> </p>

<p>*My intention is to have an array of models for each row of my custom table, and each model will include my row data, in addition to the element's content it is related to.</p>

<p><img src="https://i.sstatic.net/wWqpz.png" alt="Here is a visual helping to explain what I am trying to accomplish."></p>

CC BY-SA 3.0
2015-01-10T21:55:05.587Z
I have tried adding a select for the columns of the custom table. Also I have added a join to my table element key id to the elements.id. Because my custom table has a many to one relationship to the elements table should this work?
a-am
<plugin-development><elementcriteriamodel><queries>
Using modifyElementsQuery to get plugin's custom table data
# Using modifyElementsQuery to get plugin's custom table data

(Asked by: username_0 on 2015-01-10)

I need to join my **plugin's custom table** to a an **ElementCriteriaModel (ECM)**. I am trying to use **modifyElementsQuery**.

In the plugin's custom table, rows can share an element/entry via a separate id.

**Can I achieve this by adding to the modifyElementsQuery? What would the query be to accomplish this?**

\*My intention is to have an array of models for each row of my custom table, and each model will include my row data, in addition to the element's content it is related to.

![Here is a visual helping to explain what I am trying to accomplish.](https://i.sstatic.net/wWqpz.png)

**Tags:** plugin-development, elementcriteriamodel, queries

> **Comments:**
> **username_0**: I have tried adding a select for the columns of the custom table. Also I have added a join to my table element key id to the elements.id. Because my custom table has a many to one relationship to the elements table should this work?  

---

## 1 answers

### Answer by username_0 on 2015-01-14. Score: 1 | Accepted answer

I found out I was not able to accomplish what I needed by using **modifyElementsQuery**. So I created my own **ElementCriteriaModel** & **buildElementsQuery** for my plugin. This allows the criteria to be built from my table directly instead of elements table.

> **Comments:**
> **username_1**: Any way you could share the code you used to do this?  
> **username_0**: @JarrettBarnett I followed Crafts internal ElementCriteriaModel & ElementsService.   The customization came in with the BuildElementsQuery method you find in ElementsService.  View comments here: http://craftcms.stackexchange.com/questions/5134/what-is-the-proper-way-to-fetch-items-from-a-custom-db-table-and-output-them-in/5201#comment7467_5201 There is a link to a plugin that helped me move in the right direction.  

---

