Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter get_where order_by

I am tring to use get_where and order_by like so...

$query = $this->db->get_where($this->tbl_name, $where)->order_by('birth_date', 'ASC');

but got this error...

Fatal error: Call to undefined method CI_DB_mysql_result::order_by() in C:\xampp\htdocs\OAWA\application\models\Member_model.php on line 82

What Am i doing wrong?

like image 534
user979331 Avatar asked Nov 10 '12 20:11

user979331


1 Answers

In CodeIgniter's Active Record, every method returns the object itself (which allows method chaining) except for get and get_where which return the result set.

Thus, what you need to do is:

$query = $this->db->order_by('birth_date', 'ASC')->get_where($this->tbl_name, $where);

i.e. The get_where() call needs to be the last one. It returns the result set, and so calling order_by() after get_where() is attempting to call it on the result set which is invalid.

EDIT

There are other ways to write this query as well:

 $query = $this->db->from($this->tbl_name)->where($where)->order_by('birth_date', 'ASC')->get();
like image 181
xbonez Avatar answered Sep 30 '22 23:09

xbonez