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 & 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)?

1 Answer 1


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.