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

时间:2013-12-04 14:10:26

标签: php mysql codeigniter activerecord

我不确定为什么会这样,因为当我在另一个控制器中使用相同的模型功能时,它可以完美地工作,但不能在这个控制器中使用。

在提交<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;
    }
}

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

====更新====

我已经按照@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

1 个答案:

答案 0 :(得分:1)

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

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

将以上行添加到content.php

中的_construct()