简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM