Answers.Body: <p>Thanks Brad for breaking it down like that. It helped me take a step back and see what it was I was doing.</p>
  
  <p>Here is the relationship I have that is working:</p>
  
  <pre><code>public function defineAttributes()
  {
      return array(
          'id' =&gt; AttributeType::Number,
          'packageId' =&gt; AttributeType::Number,
          ...
      );
  }
  
  
  public function defineRelations()
  {
      return array(
          'element' =&gt; array(static::BELONGS_TO, 'ElementRecord', 'id', 'required' =&gt; true, 'onDelete' =&gt; static::CASCADE),
          'package' =&gt; array(static::BELONGS_TO, 'Commerce_PurchasableRecord', 'onDelete' =&gt; static::CASCADE),
      );
  }
  </code></pre>
  
  <p>Now I am able to have a relation to the user id <code>element</code> and back to my product <code>package</code>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-12T02:59:55.117Z
Answers.LastActivityDate: 2016-05-12T02:59:55.117Z
Answers.UserDisplayName: Damon
Body: <p>First off, <em>HUGE</em> thank you to Lindsey for opening this door for me. Opened up a whole new layer!</p>
  
  <p>I have (another) plugin that is listening to the <code>commerce_orders.onOrderComplete</code> event. My plugin inspects the order, and if a specific product was purchased, I capture that and save the pre-defined values in a custom table.</p>
  
  <p>For example, if you buy "Product A", you get to pick two "Product B" options (these options are defined in commerce/CP).</p>
  
  <p>I am able to create the custom table and save the correct values - so far so good.</p>
  
  <p>However, I would like to relate not only the user back to the user account, but the "Product A" back to the purchasable.</p>
  
  <p>Something like this:</p>
  
  <p><strong>MyPluginRecord.php</strong></p>
  
  <pre><code>public function defineAttributes()
  {
      return array(
          'id' =&gt; array(AttributeType::Number, 'default' =&gt; null),
          'packageId' =&gt; array(AttributeType::Number, 'default' =&gt; null),
          ...
      );
  }
  
  
  public function defineRelations()
  {
      return array(
          'element' =&gt; array(static::BELONGS_TO, 'ElementRecord', 'id', 'required' =&gt; true, 'onDelete' =&gt; static::CASCADE),
          'package' =&gt; array(static::BELONGS_TO, '????', 'id', 'required' =&gt; true, 'onDelete' =&gt; static::CASCADE),
      );
  }
  </code></pre>
  
  <p>I'm not sure how to get back to the commerce purchasable record?</p>
  
  <p>Thank you for any suggestions!</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-11T21:37:39.697Z
Comments.Text: If you take the Record out of the equation, you're looking for an `elementId` column in your table with a FK back to the `id` column in the  `craft_elements` table and a `packageId` column with a FK back to the `id` column in the `craft_commerce_purchasables` table?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-11T22:01:54.510Z
Comments.Text: Yes. the `package` variable is the `packageId` that I would like to relate back to the `craft_commerce_purchasables` table (of the corresponding product).
Comments.UserDisplayName: Damon
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-12T02:50:25.900Z
Comments.Text: Boom! Thanks Brad. That was just enough to help take a step back.
Comments.UserDisplayName: Damon
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-12T03:50:42.153Z
Comments.Text: Hah, sorry.  Was going to answer, but you know... life.  Glad you got it sorted!
Comments.UserDisplayName: Brad Bell
Tags: <plugin-development><plugin-craftcommerce>
Title: How can I relate a FK back to the purchasable ID?
ThreadText: # How can I relate a FK back to the purchasable ID?
  
  (Asked by: username_0 on 2016-05-11)
  
  First off, *HUGE* thank you to Lindsey for opening this door for me. Opened up a whole new layer!
  
  I have (another) plugin that is listening to the `commerce_orders.onOrderComplete` event. My plugin inspects the order, and if a specific product was purchased, I capture that and save the pre-defined values in a custom table.
  
  For example, if you buy "Product A", you get to pick two "Product B" options (these options are defined in commerce/CP).
  
  I am able to create the custom table and save the correct values - so far so good.
  
  However, I would like to relate not only the user back to the user account, but the "Product A" back to the purchasable.
  
  Something like this:
  
  **MyPluginRecord.php**
  
  ```
  public function defineAttributes()
  {
      return array(
          'id' => array(AttributeType::Number, 'default' => null),
          'packageId' => array(AttributeType::Number, 'default' => null),
          ...
      );
  }
  
  public function defineRelations()
  {
      return array(
          'element' => array(static::BELONGS_TO, 'ElementRecord', 'id', 'required' => true, 'onDelete' => static::CASCADE),
          'package' => array(static::BELONGS_TO, '????', 'id', 'required' => true, 'onDelete' => static::CASCADE),
      );
  }
  
  ```
  
  I'm not sure how to get back to the commerce purchasable record?
  
  Thank you for any suggestions!
  
  **Tags:** plugin-development, plugin-craftcommerce
  
  > **Comments:**
  > **username_1**: If you take the Record out of the equation, you're looking for an `elementId` column in your table with a FK back to the `id` column in the  `craft_elements` table and a `packageId` column with a FK back to the `id` column in the `craft_commerce_purchasables` table?  
  > **username_0**: Yes. the `package` variable is the `packageId` that I would like to relate back to the `craft_commerce_purchasables` table (of the corresponding product).  
  > **username_0**: Boom! Thanks username_1. That was just enough to help take a step back.  
  > **username_1**: Hah, sorry.  Was going to answer, but you know... life.  Glad you got it sorted!  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2016-05-12. Score: 3 | Accepted answer
  
  Thanks username_1 for breaking it down like that. It helped me take a step back and see what it was I was doing.
  
  Here is the relationship I have that is working:
  
  ```
  public function defineAttributes()
  {
      return array(
          'id' => AttributeType::Number,
          'packageId' => AttributeType::Number,
          ...
      );
  }
  
  public function defineRelations()
  {
      return array(
          'element' => array(static::BELONGS_TO, 'ElementRecord', 'id', 'required' => true, 'onDelete' => static::CASCADE),
          'package' => array(static::BELONGS_TO, 'Commerce_PurchasableRecord', 'onDelete' => static::CASCADE),
      );
  }
  
  ```
  
  Now I am able to have a relation to the user id `element` and back to my product `package`.
  
  
  ---
  
  