【问题标题】:moving specific field value to another table before deleting the whole row在删除整行之前将特定字段值移动到另一个表
【发布时间】:2014-06-30 09:41:35
【问题描述】:

我有一个包含 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

【问题讨论】:

  • 请描述您的问题...
  • 我有一个包含 7 个字段的表,当我通过 id 从表中删除一行时,我希望将该行的两个字段插入到另一个只有两列的表中。
  • 这对我来说看起来不错。你有任何错误吗?是否正在插入行?或者,删除?
  • 这就是问题所在,没有错误或警告但没有插入或删除

标签: mysql codeigniter activerecord


【解决方案1】:

您应该尝试调试代码中的一般错误,例如,
1) 尝试在index.php中将环境设置为development
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";
    }
}

【讨论】:

  • 我试过你的方法。没运气。可以这样写 $accounts = $this->accounts_model->get_accounts_data_by_id($id);并通过循环发送?这些模型功能有什么问题吗?
  • 你能发布你的功能吗get_accounts_data_by_id
猜你喜欢
  • 2016-05-27
  • 2012-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-08
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多