遇到PHP错误
严重性:注意
消息:未定义的属性:Magazine :: $ db
文件名:core / Model.php
行号:52
<?php
class MY_Model extends CI_Model {
const DB_TABLE = 'abstract';
const DB_TABLE_PK = 'abstract';
function __construct(){
parent::__construct(); // construct the Model class
}
/**
* Create record.
*/
private function insert() {
$this->db->insert($this::DB_TABLE, $this);
$this->{$this::DB_TABLE_PK} = $this->db->insert_id();
}
/**
* Update record.
*/
private function update() {
$this->db->update($this::DB_TABLE, $this, $this::DB_TABLE_PK);
}
/**
* Populate from an array or standard class.
* @param mixed $row
*/
public function populate($row) {
foreach ($row as $key => $value) {
$this->$key = $value;
}
}
/**
* Load from the database.
* @param int $id
*/
public function load($id) {
$query = $this->db->get_where($this::DB_TABLE, array(
$this::DB_TABLE_PK => $id,
));
$this->populate($query->row());
}
/**
* Delete the current record.
*/
public function delete() {
$this->db->delete($this::DB_TABLE, array(
$this::DB_TABLE_PK => $this->{$this::DB_TABLE_PK},
));
unset($this->{$this::DB_TABLE_PK});
}
/**
* Save the record.
*/
public function save() {
if (isset($this->{$this::DB_TABLE_PK})) {
$this->update();
}
else {
$this->insert();
}
}
/**
* Get an array of Models with an optional limit, offset.
*
* @param int $limit Optional.
* @param int $offset Optional; if set, requires $limit.
* @return array Models populated by database, keyed by PK.
*/
public function get($limit = 0, $offset = 0) {
if ($limit) {
$query = $this->db->get($this::DB_TABLE, $limit, $offset);
}
else {
$query = $this->db->get($this::DB_TABLE);
}
$ret_val = array();
$class = get_class($this);
foreach ($query->result() as $row) {
$model = new $class;
$model->populate($row);
$ret_val[$row->{$this::DB_TABLE_PK}] = $model;
}
return $ret_val;
}
}
?>
我的控制器是下一个
<?php
if ( ! defined("BASEPATH") ) exit("No direct script access allowed");
class Magazine extends CI_Controller {
public function index(){
$this->load->model('Publication');
$this->Publication->publication_name = 'Sandy Shore';
$this->Publication->save();
echo '<tt><pre>' . var_export( $this->Publication, TRUE) . '</pre></tt>';
$this->load->model('Issue');
$issue = new Issue();
$issue->publication_id = $this->Publication->publication_id;
$issue->issue_number=2;
$issue->issue_date_publication = date('2015-04-21');
$issue->save();
echo '<tt><pre>' . var_export( $issue, TRUE) . '</pre></tt>';
$this->load->view('magazines');
}
}
答案 0 :(得分:0)
请问您的config / autoload.php文件中是否有这行代码?
$autoload['libraries'] = array('database', 'session');
这两个是整个项目中最常用的库。