Where are Matrix Entries Fields Relationships in the Database? I can see the simple matrix field data in the craft_matrixcontent_MYMATRIXtable.

craft_matrixcontent_MYMATRIX
---------------------------
id | elementId | locale | field_BLOCKTYPE_FIELDHANDLE | field_BLOCKTYPE_FIELDHANDLE | dateCreated | dateUpdated | uid


If I need to query upon those fields, I can thanks to some of the great posts to this site. But those fields are simple text fields.

I have an Entries field in my matrix. Where can I find this relationship in the database? It doesn't exist in the craft_matrixcontent_MYMATRIX table.
I think I've figured it out. The sourceId of the craft_relations table is the elementId of the custom matrix table. And the targetId of the craft_content table is the elementId of the `craft_content table.



Code example

Here's what I was trying to do. I needed to filter entries based on a field value in an Entries matrix field on another entry. Whew.

$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = 'FIRSTCOOLTHING';

$query = craft()-&gt;elements-&gt;buildElementsQuery($criteria);

$query-&gt;join('matrixblocks matrixblocks', 'matrixblocks.ownerId = entries.id');
$query-&gt;join('matrixcontent_MYMATRIX matrixcontent_MYMATRIX', 'matrixcontent_MYMATRIX.elementId = matrixblocks.id');
$query-&gt;join('relations relations', 'relations.sourceId = matrixcontent_MYMATRIX.elementId');
$query-&gt;join('content SECONDCOOLTHING', 'SECONDCOOLTHING.elementId = relations.targetId');

$query-&gt;andWhere('SECONDCOOLTHING.field_FIELDHANDLE = '.$THINGTOSEARCHFOR);

$query-&gt;order('title asc');

$queryResults = $query-&gt;queryAll();
return EntryModel::populateModels($queryResults);