Answers.Body: <p>One or more <a href="https://craftcms.com/docs/entries-fields" rel="nofollow noreferrer">Entries Fields</a> might help you establish the relationship between your custom element and the other elements - after that, you should be able to call <a href="https://craftcms.com/classreference/models/BaseElementModel#setContentFromPost-detail" rel="nofollow noreferrer"><code>setContentFromPost()</code></a> on the Entries in that field to change the content of related elements.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-05T04:29:36.807Z
Answers.LastActivityDate: 2017-01-05T04:29:36.807Z
Answers.UserDisplayName: Sam LeBlanc
Body: <p>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.</p>
  
  <p>I've looked at the Events plugin, as well as Craft &amp; Commerce's source code, but I'm still struggling to write my <code>modifyElementsQuery</code> 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).</p>
  
  <p>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)?</p>
  
Tags: <plugin-craftcommerce><relations><database><elementtype>
Title: What's the correct way to relate element types to a custom element type?
ThreadText: # What's the correct way to relate element types to a custom element type?
  
  (Asked by: username_0 on 2016-12-19)
  
  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)?
  
  **Tags:** plugin-craftcommerce, relations, database, elementtype
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-01-05. Score: 1
  
  One or more [Entries Fields](https://craftcms.com/docs/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()`](https://craftcms.com/classreference/models/BaseElementModel#setContentFromPost-detail) on the Entries in that field to change the content of related elements.
  
  
  ---
  
  