where(['status' => 1])->andWhere(["<","expiration_time",$now])->all(); foreach ($orders as $order){ /** @var Order $order */ $goodsUserIds[] = $order->id; $orderIds[] = $order->goods_users_id; } Order::updateAll(['status' => 3], ['id' => $orderIds]); GoodsUser::updateAll(['status' => 2], ['id' => $goodsUserIds]); echo "ok"."\n"; } }