Elexer
Elexer

Reputation: 153

Creating default object from empty value Yii2

Using yii2 trying make success callback for payments. Callback work, but i need make changes for order. In my common/congig/main.php:

'successCallback' => function($invoice) {
    $order = \common\models\Checkout::findOne($invoice->order_id);
    $order->payment_status = 1;
    $order->update();
}

$invoice->order_id; receives current order id, i need change payment status for checkout model.

Update:

can I somehow run it recursively? for example, if I have several records with one ID?

Upvotes: 0

Views: 116

Answers (1)

Skatox
Skatox

Reputation: 4284

The problem in your code is that findOne() requires the name of the column to compare the value, in this case, the Checkout's ID column.

Assuming it's order_id like in invoice table.

The code will be like this:

'successCallback' => function($invoice) {
    $order = \common\models\Checkout::findOne(['order_id' => $invoice->order_id]);
    $order->payment_status = 1;
    $order->update();
}

Replace 'order_id' with the checkout's id column if it has a different name.

Update To update multiple records you could do something like this:

'successCallback' => function($invoice) {
    \common\models\Checkout::updateAll(['payment_status'=>1],['order_id' => $invoice->order_id]);
}

Make a DB backup before testing this code.

Upvotes: 2

Related Questions