我找不到错误。
我正在尝试使用 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';