<p>So I found this: <a href="https://github.com/iainurquhart/craft_introvert" rel="nofollow">https://github.com/iainurquhart/craft_introvert</a> which is exactly what I wanted to do. Hope this helps anyone else!</p>

CC BY-SA 3.0
2015-06-17T06:20:40.950Z
This one's a little bit more modern: https://github.com/boboldehampsink/reverserelations
Bob Olde Hampsink
CC BY-SA 3.0
2015-06-16T21:31:00.717Z
2015-06-16T21:31:00.717Z
Fred
<p>This might be a little complex, but here it goes:</p>

<p><strong>Goal:</strong> For any given entry, determine where it's located. (Ex. Entry XYZ exists in a news entry, global field, single (page), etc...)</p>

<p><strong>Context:</strong>
So far I figured I could go the custom SQL route until I learned more about craft internals, so what I did was create a Service that does the following query:</p>

<pre><code>    $query = craft()-&gt;db-&gt;createCommand()
        -&gt;select('element.*, detail.type, relation.fieldId AS sectionId, section.handle, section.name, section.hasUrls')
        -&gt;from('elements_i18n AS element')
        -&gt;join('relations AS relation', 'relation.sourceId = element.elementId')
        -&gt;join('elements AS detail', 'detail.id = element.elementId')
        -&gt;join('sections AS section', 'section.id = relation.fieldId AND section.hasUrls = 1')
        -&gt;where('relation.targetId = :id', array(
            ':id' =&gt; $elementID
        ));

    $results = $query-&gt;queryAll();
    $results = MyPlugin_EntryRelationshipModel::populateModels($results);
</code></pre>

<p>I'm using <code>MyPlugin_EntryRelationshipModel</code> as a Facade and it extends <code>Craft\ElementCriteriaModel</code></p>

<p><strong>Problem:</strong> This works flawlessly until a Matrix field comes into play.</p>

<plugin-development><entries><matrix><relations>
Get Entrys relationships regardless of context
# Get Entrys relationships regardless of context

(Asked by: username_0 on 2015-06-16)

This might be a little complex, but here it goes:
Goal: For any given entry, determine where it's located. (Ex. Entry XYZ exists in a news entry, global field, single (page), etc...)
Context:
So far I figured I could go the custom SQL route until I learned more about craft internals, so what I did was create a Service that does the following query:
    $query = craft()->db->createCommand()
        ->select('element.*, detail.type, relation.fieldId AS sectionId, section.handle, section.name, section.hasUrls')
        ->from('elements_i18n AS element')
        ->join('relations AS relation', 'relation.sourceId = element.elementId')
        ->join('elements AS detail', 'detail.id = element.elementId')
        ->join('sections AS section', 'section.id = relation.fieldId AND section.hasUrls = 1')
        ->where('relation.targetId = :id', array(
            ':id' => $elementID
        ));

    $results = $query->queryAll();
    $results = MyPlugin_EntryRelationshipModel::populateModels($results);

I'm using MyPlugin_EntryRelationshipModel as a Facade and it extends Craft\ElementCriteriaModel
Problem: This works flawlessly until a Matrix field comes into play.

**Tags:** plugin-development, entries, matrix, relations


---

## 1 answers

### Answer by username_0 on 2015-06-16. Score: 3

So I found this: <https://github.com/iainurquhart/craft_introvert> which is exactly what I wanted to do. Hope this helps anyone else!

> **Comments:**
> **username_1**: This one's a little bit more modern: https://github.com/boboldehampsink/reverserelations  

---

