在 null 上调用成员函数 helper() [英] Call to a member function helper() on null
问题描述
我正在尝试找出 codeigniter,但目前无法插入到我的数据库中.我一直在关注从数据库读取成功的官方文档.我的错误完整:
I am trying to figure out codeigniter and currently cannot insert into my database. I've been following the official documentation having success with reading from the DB. My error in full:
遇到未捕获的异常类型:错误
An uncaught Exception was encountered Type: Error
消息:在 null 时调用成员函数 helper()
Message: Call to a member function helper() on null
文件名:/Library/WebServer/Documents/codeChallenge/system/libraries/Form_validation.php
Filename: /Library/WebServer/Documents/codeChallenge/system/libraries/Form_validation.php
行号:147
回溯:
文件:/Library/WebServer/Documents/codeChallenge/application/models/People_model.php行:6 函数:__construct
File: /Library/WebServer/Documents/codeChallenge/application/models/People_model.php Line: 6 Function: __construct
文件:/Library/WebServer/Documents/codeChallenge/application/controllers/People.php行:7 功能:模型
File: /Library/WebServer/Documents/codeChallenge/application/controllers/People.php Line: 7 Function: model
文件:/Library/WebServer/Documents/codeChallenge/index.php 行:315函数:require_once
File: /Library/WebServer/Documents/codeChallenge/index.php Line: 315 Function: require_once
解决此错误的解决方案是什么?
What is the solution to resolve this error?
控制器:
class People extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('people_model');
$this->load->helper('url_helper');
}
public function index()
{
$data['people'] = $this->people_model->get_people();
$data['title'] = 'People';
$this->load->view('templates/header', $data);
$this->load->view('people/index', $data);
$this->load->view('templates/footer');
}
public function view($id = NULL)
{
$data['people_item'] = $this->people_model->get_people($id);
if (empty($data['people_item']))
{
show_404();
}
$this->load->view('templates/header', $data);
$this->load->view('people/view', $data);
$this->load->view('templates/footer');
}
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('age', 'Age', 'required');
$this->form_validation->set_rules('city', 'City', 'required');
$this->form_validation->set_rules('province', 'Province', 'required');
$this->form_validation->set_rules('country', 'Country', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('people/create');
$this->load->view('templates/footer');
}
else
{
$this->people_model->set_people();
$this->load->view('people/success');
}
}
}
型号:
<?php
class People_model extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_people($id = FALSE)
{
if ($id === FALSE)
{
$query = $this->db->get('people');
return $query->result_array();
}
$query = $this->db->get_where('people', array('id' => $id));
return $query->row_array();
}
public function set_people()
{
$this->load->helper('url');
$data = array (
'name' => $this->input->post('name'),
'age' => $this->input->post('age'),
'city' => $this->input->post('city'),
'province' => $this->input->post('province'),
'country' => $this->input->post('country')
);
return $this->db->insert('people', $data);
}
}
查看:
<h2>Create a record</h2>
<?php echo validation_errors(); ?>
<?php echo form_open('people/create'); ?>
<label for="name">Name</label>
<input type="text" name="name" /><br />
<label for="age">Age</label>
<input type="text" name="age" /><br />
<label for="city">City</label>
<input type="text" name="city" /><br />
<label for="province">Province</label>
<input type="text" name="province" /><br />
<label for="country">Country</label>
<input type="text" name="country" /><br />
<input type="submit" name="submit" value="Create people item" />
</form>
路线:
$route['people/create'] = 'people/create';
$route['people/(:any)'] = 'people/view/$1';
$route['people'] = 'people';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
推荐答案
在您的示例中 People_model
是一个模型,但您将其扩展为控制器.
in your example People_model
is a model, but you extend it as a Controller.
改变:
class People_model extends CI_Controller
进入
class People_model extends CI_Model
分析错误消息有帮助,它说(除其他外)
analyzing the error message helps, it says (among other stuff)
文件:/Library/WebServer/Documents/codeChallenge/application/models/People_model.php行:6 函数:__construct
File: /Library/WebServer/Documents/codeChallenge/application/models/People_model.php Line: 6 Function: __construct
检查People_model
会导致将模型扩展为控制器的错误
checking People_model
leads you to the mistake extending the model as controller
这篇关于在 null 上调用成员函数 helper()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!