• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I can't use models without using get_instance() in every function

#1
[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?


Messages In This Thread
I can't use models without using get_instance() in every function - by El Forum - 07-24-2014, 05:57 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.