【问题标题】:moving a row to another table but specific columns using active record使用活动记录将行移动到另一个表但特定列
【发布时间】:2014-06-28 06:27:34
【问题描述】:

如果我从一个表中删除一整行并将该行的两列值插入另一个表,我的模型函数结构应该是什么?我想使用活动记录。

function foo() 
{
    $this->db->truncate('to');
    $query = $this->db->get('from')->result(); // get first table
    foreach($query as $row)  // loop over results
    {
        $this->db->insert('to', $row); // insert each row to another table
    }
}

这是移动整行。我只想要两个特定的列。我该怎么办?

编辑 方法对吗?

public function refund() 
{   
   $id = $this->uri->segment(4);

   $data['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->db-insert('refund',$data);
}

【问题讨论】:

  • @AdrienXL 看看
  • 看来我做错了。它不工作

标签: mysql codeigniter codeigniter-2


【解决方案1】:

控制器:

class Controllername extends CI_Controller
{
    public function __construct()
    {
       parent::__construct();
       $this->load->model('your_model');
    }

    public function somename()
    {
       $id = 6;                                 // Row id to delete
       $this->your_model->delete_table1($id);

       $data = array(
                    array(
                     'col1' => 'val1',
                     'col2' => 'val2',
                     'col3' => 'val3',
                     ),
                    array(
                     'col1' => 'val1',
                     'col2' => 'val2',
                     'col3' => 'val3',
                     ),
                );
       $this->your_model->insert_table2($data);
    }
}


型号:

class Your_model extends CI_Model
{
    public function __construct()
    {
      parent::__construct();
      $this->db = $this->load->database('default',true);
    }

    public function delete_table1($id)
    {
        $this->db->delete('table1', array('id' => $id));
    }

    public function insert_table2($data)
    {
        $this->db->insert_batch('table2', $data);
    }
}


说明:

1) 创建了一个函数,您可以在其中调用模型函数,该函数提供了要从 table1 中删除的行的 $id 参数。
2) 第二个模型函数调用 inserts 2 rows to table2 提供数组作为参数并使用 insert_batch() 活动记录功能。
3)在做上述任务之前,别忘了在Controller构造器中load the model

【讨论】:

    【解决方案2】:

    循环内部:

    $insert = array(
                   "column_name_1" => $row->wanted_column_1,
                   "column_name_2" => $row->wanted_column_2
                    );
    
    $this->db->insert('to', $insert);
    

    编辑:

    $accounts 没有在你的函数中定义。

    试试吧:

    foreach ($data['accounts'] as $row)
    {
         $data = array(
            'barcode' => $row->barcode,
            'refunded_amount' => $row->refunded_amount
            );
    
        $this->db->insert('refund',$data);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      相关资源
      最近更新 更多