jerik
jerik

Reputation: 5767

codeigniter rest cannot make db-query?

I cannot find the error.

I am trying to use the restserver of codeingniter: https://github.com/philsturgeon/codeigniter-restserver

Implemented it as describe in the installation of restserver.

For some reason I cannot query the database: In the browser i get the errormessage

A PHP Error was encountered
Severity: Notice
Message: Undefined property: Image::$db
Filename: core/Model.php
Line Number: 51

The error seems to be in the model when doing the $query, for some reason db is not correct?

Any ideas?

Following files did i implement to make the call on: www.test.com/index.php/image/index

Model: application/models/image_model.php

<?php if (  ! defined( 'BASEPATH')) exit( 'No direct script access allowed');
class Image_model extends CI_Model {

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

    function getAll() {
        //$query = $this->db->get('images');    // Does not work. Active Record missing in libraries?
        $query = $this->db->query( 'SELECT * FROM images' );
        return $query->result(  );
    }
}
?>

Controller: application/controllers/image.php

<?php
require(APPPATH . 'libraries/REST_Controller.php');

class Image extends REST_Controller
{
    public function index_get()
    {
        $this->load->model( 'image_model');
        $images = $this->image_model->getAll();
        $this->response($images);
    }

    public function index_post()
    {
        // Create a new book
    }
}
?>

Config section database: application/config/database.php

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'foo';
$db['default']['password'] = 'bar';
$db['default']['database'] = 'testdrive';
$db['default']['dbdriver'] = 'mysql';

Upvotes: 1

Views: 1175

Answers (1)

PhearOfRayne
PhearOfRayne

Reputation: 5050

Did you autoload the database library by setting $autoload['libraries'] in application/config/autoload.php? If not you need to do this:

Open your application/config/autoload.php and find:

$autoload['libraries'] = array();

And change it to:

$autoload['libraries'] = array('database');

If you do not want to use a database connection on every page request you can also load the database library manually my calling $this->load->database();

Upvotes: 2

Related Questions