What's the correct way to relate element types to a custom element type? I'm working on a custom stock management plugin for a client's Craft Commerce site, and I need to have relations from my custom element type (used to keep a detailed log of stock changes) to the products and variants, and the user or order that modified the stock.

I've looked at the Events plugin, as well as Craft &amp; Commerce's source code, but I'm still struggling to write my modifyElementsQuery in such a way that allows me to access the content of these other elements without directly joining from the content table (which seems like a terrible way of going about it).

What's the right way of relating two element types together? Is there any example code I can look at (either in Craft core, or an open-source plugin)?
One or more Entries Fields might help you establish the relationship between your custom element and the other elements - after that, you should be able to call setContentFromPost() on the Entries in that field to change the content of related elements.