2

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!

4
  • 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? – Brad Bell May 11 '16 at 21:37
  • Yes. the package variable is the packageId that I would like to relate back to the craft_commerce_purchasables table (of the corresponding product). – Damon May 11 '16 at 22:01
  • Boom! Thanks Brad. That was just enough to help take a step back. – Damon May 12 '16 at 2:50
  • Hah, sorry. Was going to answer, but you know... life. Glad you got it sorted! – Brad Bell May 12 '16 at 3:50
3

Thanks Brad 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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