我找不到错误。
我正在尝试使用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';
答案 0 :(得分:2)
您是否通过在$autoload['libraries']
中设置application/config/autoload.php
来自动加载数据库库?如果不是,你需要这样做:
打开application/config/autoload.php
并找到:
$autoload['libraries'] = array();
并将其更改为:
$autoload['libraries'] = array('database');
如果您不想在每个页面请求上使用数据库连接,您也可以手动加载数据库库我的调用$this->load->database();