0

(codeigniter) I Want To insert data in transaction table and at the same time update customer table . I want to Add value of ChangeDue in transation table To value now_debt in Customer Table

Function in transaction Model

public function add($_iN, $_iC, $desc, $q, $qs, $_up, $_tp, $change_due, 
$cn) {
$data = ['itemName' => $_iN, 'itemCode' => $_iC, 'description' => $desc,
'quantity' => $q, 'quantitys' => $qs, 'unitPrice' => $_up, 
'totalPrice' => $_tp,'changeDue' => $change_due,'cust_id'=>$cn];
     $this->db->insert('transactions', $data);
     $this->db->set('customer.now_debt', 'customer.now_debt' + $change_due);
     $this->db->where('customer_id', $cn);
     $this->db->join('transactions', 'transactions.cust_id = 
     customer.customer_id');
    $this->db->update('customer');
    if ($this->db->affected_rows()) {
        return $this->db->insert_id();
    }
    else {
        return FALSE;
    }
}
0

Codeigniter does not support join for update in active records. You need to update your code as follows:

public function add($_iN, $_iC, $desc, $q, $qs, $_up, $_tp, $change_due, 
$cn) {
$data = ['itemName' => $_iN, 'itemCode' => $_iC, 'description' => $desc,
'quantity' => $q, 'quantitys' => $qs, 'unitPrice' => $_up, 
'totalPrice' => $_tp,'changeDue' => $change_due,'cust_id'=>$cn];
     $this->db->insert('transactions', $data);
     $updateSuccess=$this->db->query('customer JOIN transactions ON transactions.cust_id = customer.customer_id SET customer.now_debt = customer.now_debt + ? where customer_id = ? ' , array($change_due,$cn));
    if ($updateSuccess) {
        return $this->db->insert_id();
    }
    else {
        log_message('error',$this->db->error());
        return FALSE;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.