Note that there are some explanatory texts on larger screens.

plurals
  1. POMy model doesn't work after being loaded from Controller, but works well when was loaded from another model
    primarykey
    data
    text
    <p>I am a beginner PHP and codeigniter learner.</p> <p>There is a model that doesn't work after being loaded from Controller, but works well when was loaded from another model.</p> <p>I'm building an app for users to get feedback. A user may have several questions he can make to his audience.</p> <p>From coding point of view, I have a base controller "<code>MY_Controller</code>" that extends CI_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).</p> <p>I have 2 main models: <code>user_model</code> and <code>question_model</code> </p> <p>When I load the question_model from within the user_model, everything goes well and the program works fine.</p> <p>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:</p> <pre><code>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 </code></pre> <p>Can someone help me with this? I've been struggling with this for more than a day, and I can't solve it anyway.</p> <p>My base controller:</p> <pre><code>class MY_controller extends CI_Controller{ public function index() { $this-&gt;session-&gt;set_userdata('user_id', 8); //this is here just to initialize a user while in DEV $this-&gt;prepare_user(); //populates user with DB info } </code></pre> <p>My Question controller (the one that can't use the $this->question_model methods)</p> <pre><code>class Question extends MY_Controller { public function index(){ parent::index(); $active_question = $this-&gt;uri-&gt;segment(2,0); $this-&gt;load-&gt;model('Question_model'); //this line runs well, as an echo statement after this gets printed $this-&gt;Question_model-&gt;initialize($active_question); //this is the line that triggers the "can't use method error" $this-&gt;Question_model-&gt;get_answers_list(); </code></pre> <p>This is the Question_model whose methods can't be called from controller.</p> <pre><code>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-&gt;question_id = $question_id; $this-&gt;get_question_by_id(); $this-&gt;get_question_votes(); } </code></pre> <p>And finally the User_model. I only put this here because when this model loads the Question_model, everything works fine.</p> <pre><code>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-&gt;load-&gt;database(); } public function initialize($user_id) { //populates user_info and question_list $this-&gt;user_id = $user_id; $this-&gt;get_user_by_id(); $this-&gt;get_user_questions(); //this line calls the Question_model and works fine } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload