1

我找不到错误。

我正在尝试使用 codeingniter 的 restserver:https ://github.com/philsturgeon/codeigniter-restserver

按照安装restserver中的描述实现它。

由于某种原因,我无法查询数据库:在浏览器中我收到错误消息

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

执行 $query 时错误似乎在模型中,由于某种原因 db 不正确?

有任何想法吗?

我实施了以下文件来进行调用:www.test.com/index.php/image/index

模型: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(  );
    }
}
?>

控制器: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
    }
}
?>

配置节数据库:application/config/database.php

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

1 回答 1

2

$autoload['libraries']您是否通过 设置自动加载数据库库application/config/autoload.php?如果不是,您需要这样做:

打开你的application/config/autoload.php并找到:

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

并将其更改为:

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

如果您不想在每个页面请求上都使用数据库连接,您也可以手动加载数据库我的调用$this->load->database();

于 2013-01-22T17:30:08.307 回答