本文介绍了“未定义的属性” CodeIgniter代码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注Codeigniter的TutsPlus构建CMS教程。它包括以下代码:

I am following a TutsPlus "Build a CMS" tutorial for Codeigniter. It includes this bit of code:

if (!count($this->db->ar_orderby)) {
    $this->db->order_by($this->_order_by);
}

此消息的结果:

如何解决此错误?

这是<$ c $中的代码c> MY_Model.php :

<?php
class MY_Model extends CI_Model {
    protected $_table_name = '';
    protected $_primary_key = 'id';
    protected $_primary_filter = 'intval';
    protected $_order_by = '';
    public $rules = array();
    protected $_timestamps = FALSE;

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

    public function get($id = NULL, $single = FALSE){
        if ($id != NULL) {
            $filter = $this->_primary_filter;
            $id = $filter($id);
            $this->db->where($this->_primary_key, $id);
            $method = 'row';
        }
        elseif($single == TRUE) {
            $method = 'row';
        }
        else {
            $method = 'result';
        }

        if (!count($this->db->ar_orderby)) {
            $this->db->order_by($this->_order_by);
        }
        return $this->db->get($this->_table_name)->$method();
    }

    public function get_by($where, $single = FALSE){
        $this->db->where($where);
        return $this->get(NULL, $single);
    }

    public function save($data, $id = NULL){
        // Set timestamps
        if ($this->_timestamps == TRUE) {
            $now = date('Y-m-d H:i:s');
            $id || $data['created'] = $now;
            $data['modified'] = $now;
        }

        // Insert
        if ($id === NULL) {
            !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
            $this->db->set($data);
            $this->db->insert($this->_table_name);
            $id = $this->db->insert_id();
        }
        // Update
        else {
            $filter = $this->_primary_filter;
            $id = $filter($id);
            $this->db->set($data);
            $this->db->where($this->_primary_key, $id);
            $this->db->update($this->_table_name);
        }

        return $id;
    }

    public function delete($id){
        $filter = $this->_primary_filter;
        $id = $filter($id);

        if (!$id) {
            return FALSE;
        }
        $this->db->where($this->_primary_key, $id);
        $this->db->limit(1);
        $this->db->delete($this->_table_name);
    }
}


推荐答案

如果您正在使用Code Ignitor 3.x,则可以替换以下行:

if you are using Code Ignitor 3.x then you can replace these lines:

if (!count($this->db->ar_orderby)) {
    $this->db->order_by($this->_order_by);
}

具有:

if(!count($this->db->order_by($this->_order_by))) {
    $this->db->order_by($this->_order_by);
}

这篇关于“未定义的属性” CodeIgniter代码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:50