使用另一个表记录更新一个表

时间:2019-10-16 05:35:48

标签: mysql codeigniter

我有两个由budget_id加入的表格,

finance_budget表

+-----------+--------+------------+-----------------+---------------+
| budget_id | amount |    date    | transfer_status | budget_status |
+-----------+--------+------------+-----------------+---------------+
|         1 | 135000 | 2019-10-01 | Pending         | issue         |
|         2 |  25000 | 2019-10-02 | Pending         | issue         |
|         3 | 234000 | 2019-10-03 | Pending         | issue         |
|         4 | 175000 | 2019-10-03 | Pending         | issue         |
+-----------+--------+------------+-----------------+---------------+

finance_budget_issue表

+----+-----------+-----------+--------+-----------------+---------------+
| id | budget_id | office_id | amount | transfer_status | budget_status |
+----+-----------+-----------+--------+-----------------+---------------+
|  1 |         1 |       100 | 135000 | Pending         | issue         |
|  2 |         2 |       101 |  12500 | Pending         | issue         |
|  3 |         2 |       102 |  12500 | Pending         | issue         |
|  4 |         3 |       100 | 100000 | Pending         | issue         |
|  5 |         3 |       105 |  75000 | Pending         | issue         |
|  6 |         3 |       104 |  59000 | Pending         | issue         |
|  7 |         4 |       102 | 125000 | Pending         | issue         |
|  8 |         4 |       110 |  50000 | Pending         | issue         |
+----+-----------+-----------+--------+-----------------+---------------+

我尝试使用模型

将上述两个表的“ transfer_status ”从“ 待处理”更改为“ 已批准” >

控制器

public function approveIssues($id){
        $this->checkPermissions('index', 'pendingIssues');
        if(empty($id)){
            redirect('budget/pendingIssue');
        }

        if($this->Budget_model->approveIssues($id)){
            $this->session->set_flashdata('message', 'Allocation Approved successfully ..!!');
            redirect('budget/pendingIssue');
        }else{
            $this->session->set_flashdata('error', 'Error ...!!!');
            redirect('budget/pendingIssue');
        }
    }

型号

function approveIssues($id)
    {

        $this->db->update('finance_budget_issue', array('transfer_status'=>'Approved'), array('id' => $id));
        $this->db->update('finance_budget', array('transfer_status'=>'Approved'), array('budget_id' => $id));

        if ($this->db->affected_rows()) {

            $activity=FZ_Controller::activity('approve','finance_budget_issues',$id,NULL);
            $this->db->insert('finance_user_activity',$activity);


            return true;
        }
        return false;
    }

当我在视图中按“批准”按钮时,系统显示“错误”。如果我删除该行, $this->db->update('finance_budget', array('transfer_status'=>'Approved') array('budget_id' => $id)); 在我的模型中,该操作正在成功执行,并显示“ 分配已成功批准.. !! ”消息。

所需的输出

我还需要将Finance_budget表的transfer_status放入“已批准”。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此Model代码

function approveIssues($id) {

    $this->db->set('finance_budget.transfer_status','Approved');
    $this->db->set('finance_budget_issue.transfer_status','Approved');

    $this->db->where('finance_budget.budget_id', $id);
    $this->db->where('finance_budget_issue.id', $id);

    $this->db->update('finance_budget JOIN finance_budget_issue ON finance_budget.budget_id = finance_budget_issue.id');    

    if ($this->db->affected_rows()) {

        $activity=FZ_Controller::activity('approve','finance_budget_issues',$id,NULL);
        $this->db->insert('finance_user_activity',$activity);


        return true;
    }

    return false;
}

我认为这对您非常有帮助。