GROCERY CRUD:可能CALLBACK_AFTER_INSERT返回除true或false以外的值

时间:2018-07-13 10:38:48

标签: php callback grocery-crud

假设callback_after_insert像这样:

    $crud->callback_after_insert(array($this, 'load_data'));

和类似的功能:

function load_data($post_array) {
    $success = true;

    try {

        $DOCUMENT_ROOT = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']);

        $reader = ReaderFactory::create(Type::XLSX); //set Type file xlsx
        $reader->open($file_path); //open the file

        echo "<pre>";
        $i = 0;
        $j = 0;

        $this->db->trans_begin();

        foreach ($reader->getSheetIterator() as $sheet) {

            //Rows iterator

            foreach ($sheet->getRowIterator() as $row) {

                if ($i > 0) {

                    $upload_row_insert = array(
                        "psdn_psd_id" => $post_array['upd_remarks'],
                        "psdn_item_no" => $row[0],
                        "psdn_nd1" => $row[1],
                        "psdn_nd2" => $row[2],
                        "psdn_cstatus" => "Draft",
                        "psdn_crole" => "Inputter"
                    );

                    $query = $this->db->where('( (psdn_nd1 = "' . $upload_row_insert['psdn_nd1'] . '" and psdn_nd2 = "' . $upload_row_insert['psdn_nd2'] . '") or  (psdn_nd1="' . $upload_row_insert['psdn_nd2'] . '" and psdn_nd2= "' . $upload_row_insert['psdn_nd1'] . '")) and  psdn_psd_id = "' . $post_array['upd_remarks'] . '"')
                            ->get('pob_starclick_details_nd');

                    if ($query->num_rows() == 0) {
                        $this->db->insert('pob_starclick_details_nd', $upload_row_insert);
                        $j++;
                    } else {

                        $this->db->trans_rollback();
                        $rw = $j + 1;
                        global $nd_message;
                        $nd_message = 'Duplicated Data on Row ' . $rw;
                        $success = false;
                        $reader->close();

                        return $success;
                    }
                }
                ++$i;
            }
        }
        $reader->close();
    } catch (Exception $e) {
        $data = array(
            'upd_status' => 'error',
            'upd_message' => $e->getMessage()
        );
        $this->db->where('upd_id', $primary_key);
        $this->db->update('file_upload_nd', $data);
        $success = false;
    }

    return $success;
}

在这种情况下,如果数据在数据库中有重复的行,则会在$ nd_message变量上产生一条错误消息。

并使用以下命令设置自定义错误消息:

$crud->set_lang_string('insert_error', $nd_message);

但是$ nd_message的值不出现,因此杂货店在插入时产生默认消息错误消息“插入时发生错误。”这会使用户感到困惑。

对自定义错误消息有任何想法吗?

我试图用全局变量设置消息,但这不起作用。

0 个答案:

没有答案