Yii2 как восстановить модал в afterSave

У меня есть модель ProductOffer внутри, которую я использую afterSave для генерации купона.

Прямо сейчас статус нулевой, и после сохранения я хочу обновить его.

public function afterSave($insert, $changedAttributes) {
if (floatval($this->offer) >= floatval($this->product->threshold_price)) {
$coupon = false;
$createCoupon = "";
$ctr = 1;
while ($coupon == false) {
$createCoupon = $this->createCoupon(
"Offer for " . $this->customer_name . ' #' . $this->id,
$this->product->sale_price - $this->offer,
$this->product_id
);

if ($createCoupon || $ctr > 3) {
$coupon = true;
}

$ctr++;
}

$this->status = self::STATUS_ACCEPTED_COUPON_GENERATED;
$this->coupon_code = $createCoupon->code;

// todo this
// echo "Accepted automatically then send email to customer as the same time to merchant email";
} else {
$this->status = self::STATUS_REJECTED;
}

return parent::afterSave($insert, $changedAttributes);
}

Поэтому здесь на afterSave я хочу обновить статус записи и сохранить код купона.

То, что я не хочу делать, просто так.

public function afterSave($insert, $changedAttributes) {

// So basically I want to update the status in afterSave
$this->status = "What ever value rejected or accepted it depends of the outcome of generating coupon";
$this->coupon = "AddTheCoupon";

// Save or Update
$this->save();

return parent::afterSave($insert, $changedAttributes);
}

Но, похоже, это не работает для меня, и если вы собираетесь анализировать его, оно, похоже, выполняет бесконечное обновление данных, поскольку каждый save () будет проходить через afterSave ().

Есть ли другой способ сделать это?

Спасибо!

0

Решение

Вы должны использовать метод updateAttributes, который пропускает все события.

Смотрите ссылку updateAttributes ([ ‘строку field’]).

/** After record is saved
*/
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);

$this->some_field = 'new_value';
$this->updateAttributes(['some_field']);
return true;
}
1

Другие решения

Других решений пока нет …