Codeigniter order_by返回奇怪的结果

时间:2014-10-20 12:16:50

标签: php codeigniter

如果我尝试在我的控制器中获得有序的结果数组,那么:

$data1 = array();
$data1['acontentid'] = $id;
$data1['acgid']=$_POST['acgid'];
$data1['Sort'] = '0';

$data1 = $this->my_model->where('acgid', $data1['acgid'])->order_by('Sort');

我希望结果按我们的数据库列中的整数值进行升序排序,名为'排序'。但是我得到了一些奇怪的结果:

object(Acontentgallery_model)[77]
  protected 'table_name' => string 'acontentgallery' (length=15)
  protected 'key' => string 'id' (length=2)
  protected 'soft_deletes' => boolean false
  protected 'date_format' => string 'datetime' (length=8)
  protected 'log_user' => boolean false
  protected 'set_created' => boolean false
  protected 'set_modified' => boolean false
  protected 'before_insert' => 
    array (size=1)
      0 => string 'protect_attributes' (length=18)
  protected 'after_insert' => 
    array (size=0)
      empty
  protected 'before_update' => 
    array (size=1)
      0 => string 'protect_attributes' (length=18)
  protected 'after_update' => 
    array (size=0)
      empty
  protected 'before_find' => 
    array (size=0)
      empty
  protected 'after_find' => 
    array (size=0)
      empty
  protected 'before_delete' => 

///etc...

FIXED:写它的正确方法是:

$this->my_model->where('acgid', $data1['acgid'])->order_by('Sort', 'asc')->find_all();

我必须在最后添加->find_all()

3 个答案:

答案 0 :(得分:0)

'来自官方Codeigniter文档:

Lets you set an ORDER BY clause. 
The first parameter contains the name of the column you would like to order by. 
The second parameter lets you set the direction of the result. 
**`Options are asc or desc, or random.`**



$this->db->order_by("title", "desc"); 

// Produces: ORDER BY title DESC

所以它应该是:

$data1 = $this->my_model->where('acgid', $acgid)->order_by('Sort','asc');

答案 1 :(得分:0)

应该是

 $this->db->where('acgid', $acgid)->order_by('Sort')->get('table_name')->result();

答案 2 :(得分:0)

尝试

$data1 = $this->my_model->as_array()->get_by('acgid', $acgid)->order_by('Sort','asc');