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);
MyPlugin_EntryRelationshipModel as a Facade and it extends
Problem: This works flawlessly until a Matrix field comes into play.