Welcome Guest, Not a member yet? Register   Sign In
rest api to database - Call to a member function where() on a non-object
#1

[eluser]newtonianb[/eluser]
I'm trying to implement a rest api using http://net.tutsplus.com/tutorials/php/wo...igniter-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?
#2

[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
#3

[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-gui...mples.html
#4

[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.




Theme © iAndrew 2016 - Forum software by © MyBB