Welcome Guest, Not a member yet? Register   Sign In
How to run Transactions in controller?
#1

I need to use transactions in my controller. My controller method is given below. This is not working and should not be as far as I understand. So, what could be the solution? Again, is it good practice to use transactions in the controller?

Thank you in advance.

PHP Code:
public function add_payment(){
    if(
$this->request->getMethod() != 'post') return redirect()->back();
    if(!
$this->validate('addOrderPayment')) session()->setFlashdata('error'$this->validator->listErrors());
    else{
        
$oid $this->request->getPost('oid');
        if(
$this->request->getPost('odrTkn') === md5(session('token').$oid)){
            
$order = (new ODR(['gid'=> esc($oid)]))->getOrder();
            
$data = [
                
'amount'=> $this->request->getPost('amount')
            ];
            
            
$db = \Config\Database::connect(); 
            
$db->transStart();
            
service('transaction')->addTrx($data);
            
service('order')->balanceDue($oid$data['amount']);
            
$db->transComplete();
            if (
$db->transStatus() === FALSE){
                
$this->db->transRollback();
                
session()->setFlashdata('error''Something went wrong!!');
            }
            else{
                
$db->transCommit();
                
session()->setFlashdata('success''Payment added successfully!!');
            }
        }else 
session()->setFlashdata('error''Invalid request!!');
    }
    return 
redirect()->back();


Quote:The output is always the same:
Payment added successfully!!

Thank you in advance.
"Who thinks in code"
Perfectly describes who I am
mbparvez.me
Reply


Messages In This Thread
How to run Transactions in controller? - by webdevron - 04-02-2021, 05:21 AM



Theme © iAndrew 2016 - Forum software by © MyBB