CodeIgniter Forums
rest api to database - Call to a member function where() on a non-object - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: rest api to database - Call to a member function where() on a non-object (/showthread.php?tid=36420)



rest api to database - Call to a member function where() on a non-object - El Forum - 11-30-2010

[eluser]newtonianb[/eluser]
I'm trying to implement a rest api using http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/

Everything works fine until I try to use my models in my application where I start getting
"Call to a member function where() on a non-object"

In my regular application I have a controller that loads a library and that library then loads a model which performs a database operation.

Here I'm trying to go from my restapi which extends REST_CONTROLLER which extends CONTROLLER to run a library function.
======controller_myclass.php
class myClassextends REST_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('lib_myclass');

// if i try $this->load->library('database'); I get Unable to load the requested class: database
}
======lib_myclass.php
public function __construct()
{
$this->ci =& get_instance();
$this->ci->load->model('model_myclass.php');

======model_myclass.php
function needToCallThisFunction($inUsername) {
return $this->db->where('username', $inUsername)
->from(myusers)
->count_all_results() > 0;
}

So I tried to change the function as below but still got the same problem
======model_myclass.php
function needToCallThisFunction($inUsername) {
$ci = &get;_instance();
return $ci->db->where('username', $inUsername)
->from(myusers)
->count_all_results() > 0;
}


What am i missing? What in my regular application allows me to use $this-> without having to specify a ci instance?


rest api to database - Call to a member function where() on a non-object - El Forum - 12-01-2010

[eluser]smilie[/eluser]
First of all, please use code brackets so it is more readable.

For as far as I can see, your code says that where() is not known.
Is your database loaded (autoload)?

Cheers,
Smilie


rest api to database - Call to a member function where() on a non-object - El Forum - 12-01-2010

[eluser]danmontgomery[/eluser]
It's not

Code:
$this->load->library('database');

it's

Code:
$this->load->database();

First heading of the first page of the database section of the user guide.

http://ellislab.com/codeigniter/user-guide/database/examples.html


rest api to database - Call to a member function where() on a non-object - El Forum - 12-01-2010

[eluser]newtonianb[/eluser]
Thanks alot that was it! I was looking at autoload.php which loads libraries though array and never crossed my mind that the syntax would be anything different but thanks for heads up.