在删除整行之前将特定字段值移动到另一个表

时间:2014-06-30 09:41:36

标签: mysql codeigniter activerecord

我有一个包含7个字段的表,当我通过id从表中删除一行时,我希望将该行的两个字段插入另一个只有两列的表中。

这是我的控制器功能

public function refund() {

        $id = $this->uri->segment(4);

        $accounts = $this->accounts_model->get_accounts_data_by_id($id);

        foreach ($accounts as $row) {

            $data = array(
                'barcode' => $row->barcode,
                'refunded_amount' => $row->refunded_amount,
            );
        }
        $this->accounts_model->insert_refund($data);
        $this->accounts_model->delete_accounts_data($id);
    }

这些是模型函数

public function delete_accounts_data($id) {
        $this->db->where('id', $id);
        $this->db->delete('accounts');
    }
    public function insert_refund($data){
        $this->db->insert('refund', $data);
    }

假设我要删除的行

id|name|barcode|refunded_amount|commission

01|asd |2342342|        53.01  | 5.32
另一方面,我将插入

id|barcode|refunded_amount

01|2342342|     53.01

1 个答案:

答案 0 :(得分:0)

您应该尝试调试代码以解决一般错误,例如, 1)尝试将环境设置为development中的index.php 2)在函数中添加一些debugging code以检查错误。

function refund(){
    $id = $this->uri->segment(4);
    $accounts = $this->accounts_model->get_accounts_data_by_id($id);
    echo $this->db->last_query(); //will produce the query for fetching the details by id
    if( isset( $accounts ) && count( $accounts ) > 0 ){
        foreach ($accounts as $row) {
            $data = array(
                'barcode' => $row->barcode,
                'refunded_amount' => $row->refunded_amount,
            );
        }
        print_r( $data );
        $this->accounts_model->insert_refund($data);
        $this->accounts_model->delete_accounts_data($id);
    }else{
        echo "NO DATA FOUND";
    }
}