CodeIgniter Forums
I can't use models without using get_instance() in every function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: I can't use models without using get_instance() in every function (/thread-60895.html)



I can't use models without using get_instance() in every function - El Forum - 07-24-2014

[eluser]bjosborn[/eluser]
Hi, this is my first post here.

I'm using CI 2.1.4

I have autoloaded the database, and I wrote the following method:

Code:
public function verify($log) {
        $query = $this -> db -> get_where('admin', array('login' => $log), 1);
        if ($query -> num_rows() > 0) {
            $data = $query -> row();
        } else
            $data = 'bad';
        return $data;
    }

If I put this method into a controller, it works fine.
If I put the method into a model, like this:

Code:
<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Login_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    public function verify($log) {
        $query = $this -> db -> get_where('admin', array('login' => $log), 1);
        if ($query -> num_rows() > 0) {
            $data = $query -> row();
        } else
            $data = 'bad';
        return $data;
    }

}

and run it, I get:

Fatal error: Call to a member function get_where() on a non-object in C:\xampp\htdocs\foobork\application\models\Login_model.php on line 15

If I put:

Code:
$CI = & get_instance();

inside the function, and call the query like this:

Code:
$query = $CI -> db -> get_where('admin', array('login' => $log), 1);

It works fine.

I am using eclipse and a localhost xampp server - I have a build-path set to a CI system folder placed above the application level for purposes of code-hinging, that seems to work fine.

Can you shed any light on why this is happening, am I missing something I should know?


I can't use models without using get_instance() in every function - El Forum - 07-24-2014

[eluser]CroNiX[/eluser]
Are you sure you load the model before using it? Post the controller where you are using the model.


I can't use models without using get_instance() in every function - El Forum - 07-25-2014

[eluser]www.sblog.in[/eluser]
omit parent::__construct(); from the constructor then try


I can't use models without using get_instance() in every function - El Forum - 07-25-2014

[eluser]bjosborn[/eluser]
[quote author="CroNiX" date="1406257850"]Are you sure you load the model before using it? Post the controller where you are using the model.[/quote]

I wouldn't be able to call the method if the model weren't loaded - I removed the code for loading the model, and got:



A PHP Error was encountered

Severity: Notice

Message: Undefined property: Login::$login_model

Filename: controllers/login.php

Line Number: 44

Fatal error: Call to a member function verify() on a non-object in C:\xampp\htdocs\oktsoberfest\application\controllers\login.php on line 44



I can't use models without using get_instance() in every function - El Forum - 07-25-2014

[eluser]bjosborn[/eluser]
[quote author="www.sblog.in" date="1406287799"]omit parent::__construct(); from the constructor then try[/quote]

Doing this had no apparent affect on the situation. The error was exactly the same.


I can't use models without using get_instance() in every function - El Forum - 07-25-2014

[eluser]CroNiX[/eluser]
I didn't see anything else wrong with the code you posted, and you still didn't post it all so hard to tell.


I can't use models without using get_instance() in every function - El Forum - 07-25-2014

[eluser]bjosborn[/eluser]
I upgraded my system to CI 2.2.0 - it works fine now. Thanks for your suggestions!


I can't use models without using get_instance() in every function - El Forum - 07-25-2014

[eluser]Tim Brownlaw[/eluser]
Ok well seeing as upgrading to CI 2.2.0 "fixed it" indicates something was screwy in your 2.1.4 install.
Just to be blindingly obvious...

But it's good that it's working now.