如何避免错误"Object of class stdClass could not be converted to string"



我目前遇到PHP问题,我收到此错误:

Object of class stdClass could not be converted to string. 

我使用Codeigniter 3.x

这是我模型中的函数:

public function create_post($post_image){
$this->load->library('session');
$slug = url_title($this->input->post('title'));
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body'),
'category_id' => $this->input->post('category_id'),
'user_id' => $this->session->userdata('user_id'),
'post_image' => $post_image
);
return $this->db->insert('posts', $data);
}

这是控制器:

public function create(){
// Check login
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$data['title'] = 'Create Post';
$data['categories'] = $this->post_model->get_categories();
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
} else {
// Upload Image
$config['upload_path'] = './assets/#images/posts';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
$post_image = 'noimage.jpg';
} else {
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->post_model->create_post($post_image);
// Set message
$this->session->set_flashdata('post_created', 'Your post has been created');
redirect('posts');
}
}

我在功能中的模型有错误:

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

在控制器中:

$this->post_model->create_post($post_image);

我真的不知道这个问题是什么,所以任何帮助都会很棒。

尝试var_dump($data['categories']). 看起来它返回了一个对象,但你威胁它作为一个字符串。

相关内容

  • 没有找到相关文章

最新更新