Welcome Guest, Not a member yet? Register   Sign In
I can't load my model
#1

[eluser]CoderReborn[/eluser]
I'm having problems loading my model called "Answers":

Code:
class Answer extends Ugc
{
        protected $_QuestionID;
        protected $_NumComments;


    public function __construct()
    {
            echo "Starting the Answer constructor <br/>";
            $this->load->model('Ugc');      /* load parent class */
            parent::__construct();
            $this->_table = 'Answers';     /* inherited from parent */
            echo "Ending the Answer constructor <br/>";
        }
}


And here's my controller:
Code:
class Test_answers extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

   public function index()
    {
          echo "Starting the test_answers index <br/>";
          $this->load->model('Answer', 'a');
          /** more code **/
   }

}


But when I execute the controller, I only see "Starting the test_answers index".
I never get to the first line of the Answer model constructor.

Thanks for your help.
#2

[eluser]Buso[/eluser]
You need to load the class before extending it.

One way is loading the parent model but do it somewhere before.
Another way is setting __autoload() magic function to find your base model (or any other class you need, like base controllers if any) when required. A good place for this is your config.php file, because it is loaded at the very beginning of your application.

Also, have you set your error reporting to E_ALL? So you can see what's going on
#3

[eluser]CoderReborn[/eluser]
I tried autoloading the parent class ('Ugc'), but that actually made things even worse.
Now I don't even see my debug msg "Starting the test_answers index".
I just get a blank page.

I turned on error logging to level 4 in my config file and here's my log file:

DEBUG - 2010-05-31 07:53:00 --&gt; Config Class Initialized
DEBUG - 2010-05-31 07:53:00 --&gt; Hooks Class Initialized
DEBUG - 2010-05-31 07:53:00 --&gt; URI Class Initialized
DEBUG - 2010-05-31 07:53:00 --&gt; Router Class Initialized
DEBUG - 2010-05-31 07:53:00 --&gt; Output Class Initialized
DEBUG - 2010-05-31 07:53:00 --&gt; Input Class Initialized
DEBUG - 2010-05-31 07:53:00 --&gt; Global POST and COOKIE data sanitized
DEBUG - 2010-05-31 07:53:00 --&gt; Language Class Initialized
DEBUG - 2010-05-31 07:53:00 --&gt; Loader Class Initialized
DEBUG - 2010-05-31 07:53:00 --&gt; Database Driver Class Initialized


The weird thing is that I have tested other child classes of 'Ugc' (without autoloading. I load the parent in the child class constructor) and they work fine. But when I autoload 'Ugc', I just get a blank page.

Very frustrating.

Thanks for your help.
#4

[eluser]Buso[/eluser]
I ment error_reporting(E_ALL), not error logging
That way you are gonna see the error messages in the screen
You can set it in your index.php file
#5

[eluser]CoderReborn[/eluser]
error reporting is already set to E_ALL.

I figured out the problem.
The constructor for my parent class ('Ugc') was declared as "protected".
So, autoloading it would not work and cause the program to terminate.

By changing the parent class to "public" and autoloading it, things are fine now.

It would have been helpful if CI were to include better error reporting (e.g. show an error when trying to load a class that is not accessible).




Theme © iAndrew 2016 - Forum software by © MyBB