我是初学者 PHP 和 codeigniter 学习者。
有一个模型从 Controller 加载后无法工作,但从另一个模型加载后可以正常工作。
我正在为用户构建一个应用程序以获取反馈。用户可能有几个他可以向听众提出的问题。
从编码的角度来看,我有一个MY_Controller
扩展 CI_Controller 的基本控制器“”。然后我有 2 个控制器,它们扩展了我的控制器 - 主页(用户将看到的主页)和问题(查看问题的详细信息)。
我有 2 个主要型号:user_model
和question_model
当我从 user_model 中加载 question_model 时,一切顺利,程序运行良好。
但是当我从 Question 控制器中加载 question_model 时,它会运行构造函数(我已经做出了一个回显来检查它)并完成了构造函数(我再次回显来检查),但是当我调用 question_model 的方法时,我得到错误:
Fatal error: Call to a member function initialize() on a non-object in /Users/jaimequintas/Dropbox/3 CODIGO/feedbacking/application/controllers/question.php on line 17
有人可以帮我弄这个吗?我已经为此苦苦挣扎了一天多,但无论如何我都无法解决。
我的基本控制器:
class MY_controller extends CI_Controller{
public function index()
{
$this->session->set_userdata('user_id', 8); //this is here just to initialize a user while in DEV
$this->prepare_user(); //populates user with DB info
}
我的问题控制器(不能使用 $this->question_model 方法的控制器)
class Question extends MY_Controller {
public function index(){
parent::index();
$active_question = $this->uri->segment(2,0);
$this->load->model('Question_model'); //this line runs well, as an echo statement after this gets printed
$this->Question_model->initialize($active_question); //this is the line that triggers the "can't use method error"
$this->Question_model->get_answers_list();
这是无法从控制器调用其方法的 Question_model。
class Question_model extends CI_Model {
public $question_id;
public $question_text;
public $vote_count;
public $activation_date;
public $status; //Draft, Active, Archived
public $question_notes; //user notes
public $question_url; //the segment that will be added to codeigniter url feedbacking.me/"semgent"
public $answers_list; //array with answer objects
public $last_vote; //date of the last vote
public $vote_count_interval; //this is not computed with initialize, must call method when needed
public function __construct()
{
parent::__construct();
}
public function initialize($question_id)
{
//populate question from DB with: question_id, question_text, vote_count, activation_date, status
// if $question_id ==0 creates an empty question (should be followed by create_question)
$this->question_id = $question_id;
$this->get_question_by_id();
$this->get_question_votes();
}
最后是 User_model。我之所以把它放在这里是因为当这个模型加载 Question_model 时,一切正常。
class User_model extends CI_Model {
public $user_id;
public $user_email;
public $user_name;
public $plan_id;
public $questions_list; //array with question objects
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function initialize($user_id)
{
//populates user_info and question_list
$this->user_id = $user_id;
$this->get_user_by_id();
$this->get_user_questions(); //this line calls the Question_model and works fine
}