Welcome Guest, Not a member yet? Register   Sign In
Active Record order_by with get_where
#1

[eluser]wlvrn[/eluser]
I'm not sure if this is an bug but it stumped me for a bit and I thought I should report it since it's a bit confusing and, at the very least, should be added to the documentation.

When using Active Record to create database queries, using the following statement will produce an error...

$query = $this->db->get_where('categories')->order_by('category', 'asc');

PHP Fatal error: Call to undefined method CI_DB_mysql_result::order_by() in...

Changing that to ...

$query = $this->db->order_by('category', 'asc')->get_where('categories');

... results in the query being executed as you would expect.
#2

[eluser]WanWizard[/eluser]
You shouldn't confuse the order in which you write the statements of a SQL query with the order in which you have to call AR methods.

The get() always comes last, as that is the method that actually executes the query.
#3

[eluser]wlvrn[/eluser]
That's what I found out trying to figure out what was going on.

I think it would be beneficial to users if that was in the documentation as I can see that being very confusing to someone not familiar with using AR chaining. It's implied that the order is similar to how you would structure an SQL statement however since it's not specified and any misstep in that regard results in a confusing PHP error to the developer.

Something added to the get_where or the chaining section would clear this confusing up nicely.
#4

[eluser]mddd[/eluser]
The order of the 'query bits' is NOT important. But it IS important to know the query methods from the result methods.
Once you put any kind of 'get' in there, the thing you get back is no longer the query you are buiding. It is the RESULT of the query you have built.

So:
Code:
$this->db->where('name','john)->order_by('age')->limit(3)->   get('users')   ->result();
is the same as
Code:
$this->db->limit(3)->order_by('age')->where('name','john')->from('users')->   get()   ->results();

But you can't use any function that shows you results before the get() part. Or the other way around.
#5

[eluser]wlvrn[/eluser]
Yup, I understand that.

I think the problem is that it's not in the documentation. Currently, there's no such clarification in the documentation for "get" or "get_where" and, therefore, it's not clear that these statements MUST be placed at the end of the chain. I think some notes about this in the documentation would make this much more clear and easy to understand.
#6

[eluser]mddd[/eluser]
I had never thought about that but you are right. There could be a better distinction between the different kinds of methods.
#7

[eluser]danmontgomery[/eluser]
http://ellislab.com/codeigniter/user-gui...tml#select

Quote:$this->db->get();

Runs the selection query and returns the result.

Seems like it's in the documentation to me...
#8

[eluser]wlvrn[/eluser]
Yes, it is. But that's not what I'm referring to.

I'm referring to the fact that no where in the AR documentation does it state that "get" and "get_where" must be the last statement in the chain. That, coupled with the fact that "get_where" can be confused with a "get" or "where" can cause some confusion as to its placement when used with other statements such as "order_by." What I'm suggesting is that some clarification as to the positioning of those elements on the chain should be added do the documentation.




Theme © iAndrew 2016 - Forum software by © MyBB