fuel/orm — Issue #134: Extended Orm order_by() issue
Author: itsash--
Created: 2012-02-01 09:55:48+00:00
State: open

## Issue

I have a bug_tracker module. Inside this module I have a Model for my bugs table (Model_Bugs).

This model **DOES NOT** store any default ordering, but does correctly store relationships, fields, types and validation.

When I create a new instance of this model and attempt to order by a field on the the **bugs table** the Orm ignores the order of which i append my order_by statements. I would have thought the **ORDER** in which i append my order by requests (regardless of the relation/table) is the order in which the order by statement is generated; thus is **not** the case.

## Code Example

So I then attempt to create my model instance, the related tables are used for filtering the many of possible unsolved results.

```php
$bugs = \Admin_Bug_Tracker\Model_Bugs::find()
->related('project')
->related('author')
->related('owner')
->related('status')
->related('user_group')
->where('project_id', \Session::get('active_project_id'));

$bugs->order_by('user_group.name', 'ASC')
->order_by('priority', 'DESC')
->order_by('status.order', 'ASC');
```

**Results (Incorrect)**

```sql
ORDER BY `t0`.`priority` DESC, `t5`.`name` ASC, `t0`.`priority` DESC, `t4`.`order` ASC
```

So to correct this issue, I had to hard code the table name into the field...

```php
$bugs->order_by('user_group.name', 'ASC')
->order_by('admin__bug_tracker__bugs.priority', 'DESC')
->order_by('status.order', 'ASC');
```

**Results (Correct, though erroneous)**

```sql
ORDER BY `t5`.`name` ASC, `admin__bug_tracker__bugs`.`priority` DESC, `t4`.`order` ASC
```

**Nevertheless, this results in an SQL Error...**

```
Fuel\Core\Database_Exception [ 1054 ]: Unknown column 'admin__bug_tracker__bugs.priority' in 'order clause' ...
```

**And of course, the field *DOES* exist** So I noticed I should use the reference **t0** and yet again, is ignored and the results **ARE NOT** filtered in the order I am expecting.

```sql
ORDER BY `t0`.`priority` DESC, `t5`.`name` ASC, `t0`.`priority` DESC, `t4`.`order` ASC
```