从同一个控制器中调用codeigniter方法

时间:2016-07-20 15:23:39

标签: php codeigniter

我的控制器类中有一个方法,即执行某些操作等。现在我需要从生成PDF的同一个控制器中调用另一个方法。

但是当我调用第二种方法时,第一种方法还没有完成?

我正在调用第二种方法,所以这行以下的任何内容都不会完成:

$this->generate_certificate($course_id);

我错过了什么吗?我是否需要以不妨碍第一种方法继续的方式调用它。

第一种方法,相关的位|:

else if($progress == 'c') {

                // the course is complete and the user has pressed the complete button
                // on the final page of the course

                // set course as complete in the db, set completed date, set due to next due
                // as defined in course date

                // get todays date
                $todays_date = date("Y-m-d");
                // add specified months on for next due
                $months = $course_object[0]->renewal_period;
                $due_date =  date('Y-m-d', strtotime($todays_date . ' + ' . $months .' months'));

                $training_data = array();
                $training_data['user_id'] = $user_id;
                $training_data['course_id'] = $course_id;
                $training_data['due'] = $due_date;

                $this->training_model->set_course_complete($training_data);

                $this->generate_certificate($course_id);

                //certificate generated, redirect to training dashboard
                $view_data = array(
                    'page_title'    => 'Training Dashboard',
                    'user_courses'  => $this->training_model->get_user_courses($user_id),
                );

                $this->session->set_flashdata('msg', 'You have successfully completed this course!');

                $this->load->view('training/training_dashboard',$view_data);


            } 

生成PDF方法:

private function generate_certificate($course_id) {

    $this->load->model('org_model');
    $this->load->model('user_model');

    // get todays date
    $todays_date = date('d F Y');

    // get currently logged in user details
    $user_id = $this->session->userdata('user_id');
    $user_object = $this->user_model->get_user($user_id);
    $users_name =  $user_object[0]->first_name . " " . $user_object[0]->last_name;

    // course data
    $course_object = $this->training_model->get_course_data($course_id);

    // get org name
    $org_id = $this->session->userdata('org_id');
    $org_name_object = $this->org_model->get_org_name($org_id);
    $org_name = $org_name_object[0]->org_name;

    $html=" // I've removed this as its large, but works perfectly // ";

    //this the the PDF filename that user will get to download
    $pdfFilePath = "Certificate.pdf";

    //load mPDF library
    $this->load->library('m_pdf');

    //generate the PDF from the given html
    $this->m_pdf->pdf->WriteHTML($html);

    //download it.
    $this->m_pdf->pdf->Output($pdfFilePath, "D");
}

1 个答案:

答案 0 :(得分:0)

您应该在生成PDF的模型中创建方法。您可以在模型中调用其他模型方法,因此可以将generate_certificate方法放在新模型中。然后在控制器中加载该模型并调用该方法。

这不会破坏执行。

此外,如果您只需要一种方法来生成PDF,请调用模型方法。