更新两个表Codeigniter

时间:2016-06-08 09:21:36

标签: php mysql codeigniter

我正在尝试同时更新2个表tbl_loanledgertbl_journal。两个表都具有相同的列,例如modified_by, date_modified and deleted

当我尝试运行此操作时,我在此输入代码我收到错误:

  

错误号码:1052

     

字段列表中的'modified_by'列不明确

     

更新`loan_ledger`作为a,日期为b SET`modified_by` ='1',   `date_modified` ='2016-06-08',`删除`= 1 WHERE`a``id` ='823'   AND`b``id` ='823'

     

文件名:C:\ xampp \ htdocs \ system \ system \ database \ DB_driver.php

     

行号:331

这是如何在codeigniter中更新2个表的正确方法吗?

模型

public function delete($id){

  $data = array(
        'modified_by' => $this->ion_auth->user()->row()->id,
        'date_modified' => date("Y-m-d"),
        'deleted' => 1
      );

    $this->db->set($data);
    $this->db->where('a.id', $id);
    $this->db->where('b.id', $id);
    $this->db->update('loan_ledger as a, journal as b');

}

但是,如果我只是更新单个表,它正在运行,这是一个代码:

public function delete($id){

  $data = array(
        'modified_by' => $this->ion_auth->user()->row()->id,
        'date_modified' => date("Y-m-d"),
        'deleted' => 1
      );
    $this->db->where('id', $id);
    $this->db->update(tbl_loanledger, $data);

}

3 个答案:

答案 0 :(得分:5)

否使用连接只需将两个单独的更新查询写为

 // first 
    $this->db->set('a.modified_by', $this->ion_auth->user()->row()->id);
    $this->db->set('a.date_modified', date("Y-m-d"));
    $this->db->set('a.deleted', 1);
    $this->db->where('a.id',  $id);
    $this->db->update('loan_ledger as a');
    // second
    $this->db->set('b.modified_by', $this->ion_auth->user()->row()->id);
    $this->db->set('b.date_modified', date("Y-m-d"));
    $this->db->set('b.deleted', 1);
    $this->db->where('b.id',  $id);
    $this->db->update('journal as b');

答案 1 :(得分:1)

试试这个

public function delete($id){

$data = array(
    'a.modified_by' => $this->ion_auth->user()->row()->id,
    'a.date_modified' => date("Y-m-d"),
    'b.modified_by' => $this->ion_auth->user()->row()->id,
    'b.date_modified' => date("Y-m-d"),  
    'deleted' => 1
  );

$this->db->set($data);
$this->db->where('a.id', $id);
$this->db->where('b.id', $id);
$this->db->update('loan_ledger as a, journal as b');

}

答案 2 :(得分:0)

<?php
$this->db->set('a.modified_by', $this->ion_auth->user()->row()->id);
$this->db->set('a.date_modified', date("Y-m-d"));
$this->db->set('a.deleted', 1);

$this->db->set('b.modified_by', $this->ion_auth->user()->row()->id);
$this->db->set('b.date_modified', date("Y-m-d"));
$this->db->set('b.deleted', 1);

$this->db->where('a.id',  $id);
$this->db->where('a.id = b.id');
$this->db->update('loan_ledger as a, journal as b');
?>