当我在主索引上提交表单时,我试图将变量调用$store_id
传递给同一控制器上的一个函数,名为 do_upload_image($ store_id)
我正在尝试获取插入我的商店表格中的最后一个ID。
加载另一个模型函数$ this-> example_model-> add($ store_id);但是在同一个控制器上的另一个功能上执行它时不会。
错误输出
Column 'store_id' cannot be null
INSERT INTO `setting` (`store_id`, `group`, `key`, `value`) VALUES (NULL, 'config', 'config_image', 'flagnz.png')
喜欢这样
$store_id = $this->model_store_add->add_store(); // this way should let me get id that was inserted.
$this->do_upload_image($store_id);
我怎么能让$ store_id通过同一个控制器上的另一个函数?我有一个模型试图将商店ID传递给do_upload_image
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Store_add extends MX_Controller {
public $store_id;
public function __construct() {
parent::__construct();
if (!$this->session->userdata('isLogged')) {
redirect('admin');
}
$this->load->model('admin/setting/model_store_add');
$this->load->model('admin/setting/model_store_get');
$this->lang->load('admin/setting/store_add', $this->settings->get('config_admin_language'));
}
public function index()
$this->load->library('form_validation');
$this->form_validation->set_rules('config_image', 'Store Image','callback_do_upload_image');
if ($this->form_validation->run($this) == FALSE) {
return $this->load->view('setting/store_add.tpl');
} else {
// $store id lets me get id that was inserted from store table
$store_id = $this->model_store_add->add_store();
$this->model_store_add->add_config_meta_title($store_id); // Works
$this->do_upload_image($store_id);
$this->session->set_flashdata('success', $this->lang->line('text_success'));
redirect('admin/setting/store');
}
}
public function do_upload_image($store_id) {
$config['upload_path'] = './image/upload/';
$config['allowed_types'] = $this->settings->get('config_file_ext_allowed');
$config['max_size'] = $this->settings->get('config_file_max_size');
$config['overwrite'] = $this->settings->get('config_file_overwrite');
$config['max_width'] = '*';
$config['max_height'] = '*';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (is_uploaded_file($_FILES['config_image']['tmp_name'])) {
if (!$this->upload->do_upload('config_image')) {
$this->form_validation->set_message('do_upload_image', $this->upload->display_errors('<b>config_image</b>' .' '));
return false;
} else {
$resize_data = $this->upload->data();
$config_resize['image_library'] = 'gd2';
$config_resize['source_image'] = './image/upload/' . $resize_data['file_name'];
$config_resize['new_image'] = './image/cache/' . $resize_data['file_name'];
$config_resize['maintain_ratio'] = false;
$config_resize['create_thumb'] = false;
$config_resize['width'] = 100;
$config_resize['height'] = 100;
$this->load->library('image_lib', $config_resize);
$this->image_lib->initialize($config_resize);
if (!$this->image_lib->resize()) {
$this->sessions->set_flashdata('error', $this->image_lib->display_errors());
redirect('admin/setting/setting');
} else {
$this->model_store_add->add_config_image($store_id, $this->upload->data());
}
}
}
}
}
do_upload_image上的模型
public function add_config_image($store_id = 0, $file_data = array()) {
$file_data = $this->upload->data();
$data = array(
'store_id' => $store_id,
'group' => "config",
'key' => "config_image",
'value' => $file_data['file_name']
);
$this->db->set($data);
$this->db->insert($this->db->dbprefix. 'setting');
}
模型添加
public function add_store() {
$data = array(
'name' => $this->input->post('config_name'),
'url' => $this->input->post('config_url'),
'ssl' => $this->input->post('config_ssl')
);
$this->db->set($data);
$this->db->insert_id();
$this->db->insert($this->db->dbprefix . 'store');
return $this->db->insert_id();
}
答案 0 :(得分:0)
我缩小了自己的问题。我需要第二次传递商店ID作为if语句,现在所有工作
我刚刚删除了resize函数进行测试,看看是否存在问题,但现在没有原因。还需要在回调函数下面添加return true。
public function do_upload_image($store_id) {
$config['upload_path'] = './image/upload/';
$config['allowed_types'] = $this->settings->get('config_file_ext_allowed');
$config['max_size'] = $this->settings->get('config_file_max_size');
$config['overwrite'] = $this->settings->get('config_file_overwrite');
$config['max_width'] = '*';
$config['max_height'] = '*';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('config_image')) {
if ($store_id) {
$this->model_store_add->add_config_image($store_id, $this->upload->data());
}
return true;
} else {
return false;
}
}