Codeigniter在另一个Classe的方法

时间:2018-03-17 09:55:01

标签: php codeigniter crm

非常具体 - 有一个用Codeigniter编写的称为Rise的CRM系统。每当有人手动登录(call save() method of an Expenses class)时,我想(自动)进行费用输入(inside save_timelog() method of a Projects class)。

费用控制器:

function save() {

    validate_submitted_data(array(
        "id" => "numeric",
        "expense_date" => "required",
        "category_id" => "required",
        "amount" => "required"
    ));

    $id = $this->input->post('id');

    $target_path = get_setting("timeline_file_path");
    $files_data = move_files_from_temp_dir_to_permanent_dir($target_path, "expense");
    $has_new_files = count(unserialize($files_data));

    $data = array(
        "expense_date" => $this->input->post('expense_date'),
        "title" => $this->input->post('title'),
        "description" => $this->input->post('description'),
        "category_id" => $this->input->post('category_id'),
        "amount" => unformat_currency($this->input->post('amount')),
        "project_id" => $this->input->post('expense_project_id'),
        "user_id" => $this->input->post('expense_user_id'),
        "files" => $files_data
    );

    <.. ETC. CHECKING FILES ..>

    $save_id = $this->Expenses_model->save($data, $id);

    if ($save_id) {
        echo json_encode(array("success" => true, "data" => $this->_row_data($save_id), 'id' => $save_id, 'message' => lang('record_saved')));
    } else {
        echo json_encode(array("success" => false, 'message' => lang('error_occurred')));
    }

}

项目管理员:

function save_timelog() {

    $this->access_only_team_members();
    $id = $this->input->post('id');

    $start_time = $this->input->post('start_time');
    $end_time = $this->input->post('end_time');
    $note = $this->input->post("note");
    $task_id = $this->input->post("task_id");


    if (get_setting("time_format") != "24_hours") {
        $start_time = convert_time_to_24hours_format($start_time);
        $end_time = convert_time_to_24hours_format($end_time);
    }

    $start_date_time = $this->input->post('start_date') . " " . $start_time;
    $end_date_time = $this->input->post('end_date') . " " . $end_time;

    $start_date_time = convert_date_local_to_utc($start_date_time);
    $end_date_time = convert_date_local_to_utc($end_date_time);

    $data = array(
        "project_id" => $this->input->post('project_id'),
        "start_time" => $start_date_time,
        "end_time" => $end_date_time,
        "note" => $note ? $note : "",
        "task_id" => $task_id ? $task_id : 0,
    );

    if (!$id) {
        //insert mode
        $data["user_id"] = $this->input->post('user_id') ? $this->input->post('user_id') : $this->login_user->id;
    } else {
        //edit mode
        //check edit permission
        $this->check_timelog_updte_permission($id);
    }

    $save_id = $this->Timesheets_model->save($data, $id);
    if ($save_id) {
        echo json_encode(array("success" => true, "data" => $this->_timesheet_row_data($save_id), 'id' => $save_id, 'message' => lang('record_saved')));
    } else {
        echo json_encode(array("success" => false, 'message' => lang('error_occurred')));
    }

}

所以我现在要做的就是在这些行的下方的Projects controller,save_timelog()方法中:

&LT; ...&GT;

    if (!$id) {
        //insert mode
        $data["user_id"] = $this->input->post('user_id') ? $this->input->post('user_id') : $this->login_user->id;
    } else {
        //edit mode
        //check edit permission
        $this->check_timelog_updte_permission($id);
    }

    /* CREATING A SAMPLE ARRAY WITH STATIC DATA FOR AN EXAMPLE EXPENSE ENTRY */

    $a = array(
        "expense_date" => '2018-03-13',
        "title" => 'Cat Food',
        "description" => 'Sheba, Felix, KiteKat',
        "category_id" => '85',
        "amount" => '500',
        "project_id" => '84',
        "user_id" => '10',
        "files" => $files_data
    );

    /* TRYING TO SAVE/SEND EXAMPLE ARRAY TO Expenses Class save() method (?) */

    $b = $this->Expenses_model->save($a);

    /* RESULT (?) */

    $save_id = $this->Timesheets_model->save($data, $id);

    if ($save_id) {
        echo json_encode(
          array(
            array(
              "success" => true,
              "data" => $this->_timesheet_row_data($save_id),
              'id' => $save_id,
              'message' => lang('record_saved')
            ),
            array(
              "success" => true,
              "data" => _row_data($b),
              'id' => $save_id,
              'message' => lang('record_saved')
            )
          )
        );
    } else {
        echo json_encode(array("success" => false, 'message' => lang('error_occurred')));
    }

&lt; ..关闭save_timelog()方法..&gt;

然而,它肯定不起作用,我得到的只是"POST http://rise.test/index.php/projects/save_timelog 500 (Internal Server Error)"

我还在Projects _construct()中加载费用模型和费用类别模型:

项目管理员:

public function __construct() {
    parent::__construct();
    $this->load->model("Project_settings_model");
    $this->load->model("Expense_categories_model");
    $this->load->model("Expenses_model");
}

我还联系了Rise的开发人员,提出以下问题/答案:

  

我:

     

在Projects controller save_timelog()方法中我只想调用   费用控制器save()方法,如果save_timelog()成功   我想保存费用($ this-&gt; Expenses_model-&gt; save($ data,   $ ID); )有适当的数据。可能是现在的静态值   save()方法中的$ data数组 - 只是为了找出它的工作原理。

     

Rise Devs:

     你好,你做的几乎是正确的。只需删除第二个参数$ id即可。它   应该只用于更新。 $这 - &GT; Expenses_model-&GT;保存($数据)

非常感谢任何帮助和指示!感谢。

0 个答案:

没有答案