/** * @inheritdoc */ public function subscribe($subscriber_id, $label = null, $categories = null, $exceptions = null) { $trx = models\DbSubscription::getDb()->transaction !== null ? null : models\DbSubscription::getDb()->beginTransaction(); $subscription = models\DbSubscription::find()->withQueue($this->id)->withSubscriber($subscriber_id)->one(); if ($subscription === null) { $subscription = new models\DbSubscription(); $subscription->setAttributes(['queue_id' => $this->id, 'subscriber_id' => $subscriber_id, 'label' => $label]); } else { $subscription->is_deleted = 0; } if (!$subscription->save()) { throw new Exception(Yii::t('app', 'Failed to subscribe {subscriber_id} to {queue_label}', ['subscriber_id' => $subscriber_id, 'queue_label' => $this->label])); } $this->saveSubscriptionCategories($categories, $subscription->primaryKey, false); $this->saveSubscriptionCategories($exceptions, $subscription->primaryKey, true); if ($trx !== null) { $trx->commit(); } return true; }