How can I use insert_id from one method in model in another method in the same model

丶灬走出姿态 提交于 2019-12-02 18:11:45

问题


I have a problem with insert_id. I'm using CodeIgniter. I have a method in which I insert some values and in it I return

$insert_id=$this->db->insert_id();
return $insert_id;`

I want to use this value in another method in the same model. I tried that: $insert_id=$this->add_teacher_subject(); but in this way first method runs again and I doesn't receive last_insert_id , but this value +1. Please, tell me how could I solve this problem?

My model is:

 public function add_teacher_subject() {

        $date = new DateTime("now"); 
        $data=array(
            'teacher_id'=>$this->input->post('teacher'),

            'user_id'=>$this->session->userdata['user_id'],

            'created_at'=>$date->format('Y-m-d H:i:s')

        );

        $this->db->insert('student_surveys',$data);
       $insert_id=$this->db->insert_id();
        return $insert_id;

    }
  public function survey_fill() 
    {
          $insert_id=$this->add_teacher_subject();
        if ($this->input->post('answer')) {
            $answers= $this->input->post('answer');           
            if (null !==($this->input->post('submit'))) {
                $date = new DateTime("now"); 
                foreach($answers as $question_id=>$answer)
                {
                    $data = array(
                        'user_id'=>$this->session->userdata['user_id'],
                        'question_id'=>$question_id,
                        'answer'=>$answer,              

                        'student_survey_id' => $insert_id
                    );

                    $this->db->insert('survey_answers', $data);

                }

            }

回答1:


You can use SESSION[] for keeping last insert id:

public function add_teacher_subject() {
    $date = new DateTime("now");
    $data = array(
        'teacher_id' => $this->input->post('teacher'),
        'user_id' => $this->session->userdata['user_id'],
        'created_at' => $date->format('Y-m-d H:i:s')
    );

    $this->db->insert('student_surveys', $data);
    $insert_id = $this->db->insert_id();
    $newdata = array('insert_id' => $insert_id);
    $this->session->set_userdata($newdata); 
    return $insert_id;
}

public function survey_fill() {
    $insert_id = $this->session->userdata('insert_id');
    if ($this->input->post('answer')) {
        $answers = $this->input->post('answer');
        if (null !== ($this->input->post('submit'))) {
            $date = new DateTime("now");
            foreach ($answers as $question_id => $answer) {
                $data = array(
                    'user_id' => $this->session->userdata['user_id'],
                    'question_id' => $question_id,
                    'answer' => $answer,
                    'student_survey_id' => $insert_id
                );

                $this->db->insert('survey_answers', $data);
            }
        }
    }
}


来源:https://stackoverflow.com/questions/29992891/how-can-i-use-insert-id-from-one-method-in-model-in-another-method-in-the-same-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!