我在使用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

最佳答案

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

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

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


“ order子句”中的“”列未知

应该是这样的:

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

关于php - CodeIgniter:CRUD order_by错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20114431/

10-11 11:33