Welcome Guest, Not a member yet? Register   Sign In
Constructor Help
#1

[eluser]bunoire14[/eluser]
Hi Guys,

hope this is the right place to post,

I have an issue when trying to load a model from within the class constructor, it doesn't throw any errors but just gives me a blank page.

Am i doing something incorrectly?

Code:
function __construct()
    {
        parent::__construct();
        $this->load->model('user_model');
    }

Any help greatly appreciated!
#2

[eluser]danmontgomery[/eluser]
Have you checked the apache error log for PHP errors?
#3

[eluser]fesweb[/eluser]
You need to get the CodeIgniter super-object first...
Code:
function __construct()
    {
        parent::__construct();
        // get CI Object instance
        $this->ci =& get_instance();
        // then load using that instance
        $this->ci->load->model('user_model');
    }

From the user guide:
Quote:Note: You'll notice that the above get_instance() function is being passed by reference:

$CI =& get_instance();

This is very important. Assigning by reference allows you to use the original CodeIgniter object rather than creating a copy of it.
#4

[eluser]InsiteFX[/eluser]
If he is loading the model from the controller he doe's not need to use the CI Superobject!

If he trying to load it in a library etc then yes he will need the CI Superobject.

InsiteFX
#5

[eluser]fesweb[/eluser]
InsiteFX: Right - I missed that part since he was loading another model...oops...
#6

[eluser]joakley77[/eluser]
Have you already checked to make sure you named the model correctly? I've been known to forget to correctly name the model controller before.

IE
[code]
<?php
// application/models/user_model.php
class User_model extends CI_Model {
// constructor/methods/etc
}
?>
#7

[eluser]InsiteFX[/eluser]
Here is a tip for all new users:
Code:
// use this at the top of all your controllers libraries and models.
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// not just this. With this they can view your scripts!
<?php

// Also do not use the PHP closing tag ?> This causes problems with white space.

// use this at the bottom.
/* End of file your_name.php */
/* Location: ./application/directory/file_name.php */

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB