CodeIgniter错误:查询数据库时试图获取非对象的属性


CodeIgniter Error: Trying to get property of non-object when querying a database

我不确定为什么会发生这种情况,因为当我在另一个控制器中使用相同的模型函数时,它可以完美地工作,但在这个控制器中却不行。

在提交<form method="POST"/>时,我正在控制器中进行一些检查,以便在将项目插入数据库之前自动分配排序值。

controllers/content.php:

class Content extends Backend_Controller{
    public function __construct(){
        parent::__construct();
        $this->activeComponent = 1;
    }
    // FUNCTIONS BEFORE
    public function form($id = FALSE){
        // Some another $item['fields'] gathered from $this->input->post('form_fields');
        $item['catid']      = $this->input->post('form_catid');     // Actually it = 5
        $item['ordering']   = $this->input->post('form_ordering');  // Actually it = '' (empty string)
        // Auto-ordering
        if($item['ordering'] == ''){
            $item['ordering'] = $this->categories_model->auto_ordering($this->activeComponent, $item['catid']);
            return var_dump($item); // DEBUG4
        }
    }
    // FUNCTIONS AFTER
}

这是我的模态检查/core/MY_Model.php:

// My base model shared with each custom models
class MY_Model extends CI_Model{
    protected   $_table_name        = '';
    public function auto_ordering($component, $level){
        var_dump('Component: '. $component); // DEBUG1
        var_dump('Level: '. $level); // DEBUG2
        $this->_table_name = 'categories';
        // Get know the last item in a set with the highest ORDERING value
        $ordering = $this->db
            ->select('ordering')
            ->where('cid', $component)
            ->where('pid', $level)
            ->order_by('ordering DESC')
            ->get($this->_table_name)
            ->row();
        var_dump($this->db->last_query()); // DEBUG3
        $last_order = intval($ordering->ordering);
        // Increase by 1
        return ++$last_order;
    }
}

在为"类别"而不是"内容"添加新记录时,我使用了相同的检查方法。我不知道为什么我不能检索这些值。在/core/MY_Model.php中执行var_dump($ordering);时,我得到一个空数组。有人看到这里有什么不对劲吗?

===更新======

根据@SuthanBala的建议,我在代码中添加了一些调试信息,所以这里有一个转储:

// DEBUG1
string 'Component: 1' (length=12)
// DEBUG2
string 'Level: 5' (length=8)
// DEBUG3
string 'SELECT `ordering`
FROM (`cis_categories`)
WHERE `cid` =  '1'
AND `pid` =  '5'
ORDER BY `ordering` DESC' (length=102)
// ERROR
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: core/MY_Model.php
Line Number: 197
// DEBUG4
array (size=10)
    'catid' => string '5' (length=1)
    'rid' => int 0
    'alvl' => string '10' (length=2)
    'ordering' => int 1
    'state' => string '0' (length=1)
    'title' => string 'article3' (length=8)
    'slug' => string 'article3' (length=8)
    'text' => string '<p>sad</p>' (length=10)
    'aid' => string '1' (length=1)
    'pubdate' => string '2013-12-04' (length=10)

===更新2====

这是另一个controllers/categories.php的转储,它在那里工作得很好:

// DEBUG1
string 'Component: 1' (length=12)
// DEBUG2
string 'Level: 2' (length=8)
// DEBUG3
string 'SELECT `ordering`
FROM (`cis_categories`)
WHERE `cid` =  '1'
AND `pid` =  '2'
ORDER BY `ordering` DESC' (length=102)
array (size=7)
  'cid' => string '1' (length=1)        // $component
  'pid' => string '2' (length=1)        // $level
  'title' => string 'Article 5th' (length=11)
  'slug' => string 'article-5th' (length=11)
  'image' => string 'no-image.jpg' (length=12)
  'description' => string '<p>This should be automatically ordered to the 5th position.</p>' (length=64)
  'ordering' => int 5

您需要在控制器中加载模型。

$this->load->model('categories_model');

将以上行添加到content.php 中的_construct()