Welcome Guest, Not a member yet? Register   Sign In
Load model in library
#1

[eluser]SammyO[/eluser]
I think I've searched the entire world wide web, but I can't find a solution to my problem.

I want to load a model into a library.

screen_model.php:
Code:
class Screen_model extends CI_Model {
    function __construct() {
      $this->load->database();
    }
    
    public function add_records($UserName, $text) {
  $query = array('UserName' => $UserName, 'ContentText' => $text);
    $this->db->insert('twitter', $query);
    }
  }


The constructor of my library has the following code:
Code:
$CI =& get_instance();
$CI->load->model('screen_model', '', TRUE);

And I want to call this model by doing:
Code:
$CI->Screen_model->add_records($screen_name, $text);

This gives me the following error:
Fatal error: Call to a member function add_records() on a non-object.

What am I doing wrong?
#2

[eluser]cideveloper[/eluser]
first try switching this

Code:
$CI->Screen_model->add_records($screen_name, $text);

to this

Code:
$CI->screen_model->add_records($screen_name, $text);

might be a problem with case sensitivity

If that doesnt work, keep the lower case and switch all instances of $CI to $this->ci

for example

Code:
$CI =& get_instance();
$CI->load->model('screen_model', '', TRUE);

Code:
$this->ci =& get_instance();
$this->ci->load->model('screen_model', '', TRUE);

and then use

Code:
$this->ci->screen_model->add_records($screen_name, $text);
#3

[eluser]SammyO[/eluser]
Did the trick! Thank you very much!
Can you explain the difference between $CI and $this->CI?




Theme © iAndrew 2016 - Forum software by © MyBB