Any reason why we cant set easily the site_id programmatically?

My case is I am running a site with 4 multisite, and each site can make a stripe payment.

I added the active site siteId to the Stripe payload so that I can grab it back inside my stripe webhook. However i cant set the site_id to my model easily:

  • using the method ::create totaly ignore the value
  • using $instance->site_id = $siteId; $instance->save() does not work
  • using $instance->site_id = $siteId; $instance->forceSave() does not work
  • only - using $instance->site_id = $siteId; $instance->saveQuietly() does work

I’m curious if I am doing things wrong or if there is a better way?

What type of model is this?

kind of a standard model, in that case it is


/**
 * UserSubscription Model
 */
class UserSubscription extends Model
{
    use \October\Rain\Database\Traits\Validation;
    use \October\Rain\Database\Traits\Multisite;

    /**
     * @var array Attributes that can be propagated across sites
     */
    protected $propagatable = [];

...
}

This is likely because the site_id is set automatically by the internal events (set in the Multisite trait). Can you show me the code you are using that wants to set the site_id?

in this exact case, we use a ::create

 // Create subscription record
            $userSubscription = UserSubscription::create([
                'site_id' => $paymentIntent['metadata']['site_id'],
                'user_id' => $user->id,
                'plan_id' => $plan->id,
                'stripe_subscription_id' => 'promptpay_' . $paymentIntent['id'], // Unique identifier
                'stripe_customer_id' => $paymentIntent['customer'],
                'stripe_status' => 'active',
                'current_period_start' => $startDate,
                'current_period_end' => $endDate,
                'amount' => $paymentIntent['amount'] / 100, // Convert from cents
                'currency_id' => $plan->currency_id,
                'stripe_metadata' => json_encode([
                    'payment_type' => 'promptpay',
                    'payment_intent_id' => $paymentIntent['id'],
                    'plan_id' => $plan->id,
                    'site_id' => $paymentIntent['metadata']['site_id'],
                ])
            ]);
            $userSubscription->site_id = $paymentIntent['metadata']['site_id'];
            $userSubscription->saveQuietly();

I noticed that the same code works with using a DT::table('name')->insert([]) which would confirm your above syaing.

1 Like

Yes, we’ll need to check if it has been set externally (as above) and use that value, rather than setting the site ID directly from the site management context.

Originally, you were expected to call this, ratner than set it on the model:

Site::applyActiveSiteId($paymentIntent['metadata']['site_id']);

You should probably try that to see if it helps.

1 Like