[英]My model doesn't work after being loaded from Controller, but works well when was loaded from another model
I am a beginner PHP and codeigniter learner. 我是PHP和codeigniter的初学者。
There is a model that doesn't work after being loaded from Controller, but works well when was loaded from another model. 有一个模型从Controller加载后无法正常工作,但从另一个模型加载后则可以正常工作。
I'm building an app for users to get feedback. 我正在构建一个供用户获取反馈的应用程序。 A user may have several questions he can make to his audience. 用户可能会对他的听众提出几个问题。
From coding point of view, I have a base controller " MY_Controller
" that extends CI_Controller. 从编码的角度来看,我有一个扩展CI_Controller的基本控制器“ MY_Controller
”。 I then have 2 controllers, that extend my controller - home (the main page users will see) and questions (to view the details of a question). 然后,我有2个控制器,它们扩展了我的控制器-主页(用户将看到的主页)和问题(以查看问题的详细信息)。
I have 2 main models: user_model
and question_model
我有2个主要模型: user_model
和question_model
When I load the question_model from within the user_model, everything goes well and the program works fine. 当我从user_model内部加载question_model时,一切顺利,程序运行正常。
But when I load the question_model from within the Question controller, it runs the constructor (I've made an echo to check that) and it finishes the constructor (again I echoed to check), but when I invoke a method of the question_model I get the error: 但是,当我从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
Can someone help me with this? 有人可以帮我弄这个吗? I've been struggling with this for more than a day, and I can't solve it anyway. 我已经为此努力了一天多,但我还是无法解决。
My base controller: 我的基本控制器:
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
}
My Question controller (the one that can't use the $this->question_model methods) 我的Question控制器(不能使用$ 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();
This is the Question_model whose methods can't be called from controller. 这是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();
}
And finally the User_model. 最后是User_model。 I only put this here because when this model loads the Question_model, everything works fine. 我仅将其放在此处,因为当此模型加载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
}
When loading a model within a model, you need to get an instance of code igniter (instead of $this): 在模型中加载模型时,您需要获取代码点火器的实例(而不是$ this):
$CI =& get_instance();
$CI->load->model('Question_model');
$CI->Question_model->initialize($active_question);
$CI->Question_model->get_answers_list();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.