CodeIgniter:CRUD order_by错误

时间:2013-11-21 06:59:16

标签: php mysql codeigniter

我的CRUD方法有错误。任何人都可以帮助我。

JS_Model.php

class JS_Model extends CI_Model
{

    protected $_table_name = '';
    protected $_primary_key = 'id';
    protected $_primary_filter = 'intval';
    protected $_order_by = '';
    protected $_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);
  }

}

page_m.php

class Page_m extends JS_Model
{

    protected $_table_name = 'pages';
    protected $_primary_key = 'id';
    protected $_primary_filter = 'intval';
    protected $_order_by = 'order';
    protected $_rules = array();
    protected $_timestamps = FALSE;

}

page.php文件

class Page extends Frontend_Controller
{


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

        $this->load->model('page_m');
    }

    public function index()
    {

        $pages = $this->page_m->get();

        //var_dump($pages);
        print_r($pages);

    }
}

错误消息如下

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Page::$order_by

Filename: core/Model.php

Line Number: 51

A Database Error Occurred

Error Number: 1054

Unknown column '' in 'order clause'

SELECT * FROM (`pages`) ORDER BY `

Filename: C:\xampp\htdocs\my-cms\system\database\DB_driver.php

Line Number: 330

2 个答案:

答案 0 :(得分:1)

$this->db->order_by($this->order_by);   

应该是

 $this->db->order_by($this->_order_by);   

答案 1 :(得分:1)

消息:未定义的属性:Page :: $ order_by

在这种情况下,请使用_order_by代替order_by,如下所示:

$this->db->order_by($this->_order_by); 

'order clause'中的未知列''

这应该是这样的:

SELECT * FROM (`pages`) ORDER BY `column_name`; //column_name was missing