rsanchez/Deep

Entry Model scopeFixedOrder throws an error

Closed this issue · 1 comments

Using Entry::fixedOrder($arrayOfEntryIds) throws this SQL error: Unknown column 'FIELD(68360, 88297, 325, 704477, 448260, 454522)' in 'order clause'.

It is my understanding that to use FIELD() in this way, you need to provide the function a column to check against: eg. FIELD(id, 1, 3, 5, 4). For most channel entry queries, I think we could get away with something like FIELD(exp_channel_titles.entry_id, 1, 3, 5, 4).

Also, Eloquent's orderBy scope forces a sort direction which we don't want and will trigger an error itself. eg. order by FIELD(68360, 88297, 325, 704477, 448260, 454522) asc. We can get around this by using orderByRaw instead.

I would propose something like this:

    public function scopeFixedOrder(Builder $query, $fixedOrder)
    {
        $fixedOrder = is_array($fixedOrder) ? $fixedOrder : array_slice(func_get_args(), 1);

        call_user_func_array([$this, 'scopeEntryId'], func_get_args());

        return $query->orderByRaw('FIELD(exp_channel_titles.entry_id,'.implode(', ', $fixedOrder).')');
    }

Let me know if you want me to make a PR for this or if I'm missing something crucial here.

This makes a whole lotta sense. A PR would be good.

One note: we shouldn't hardcode the exp_ prefix on the table name, it's configurable in EE. I forget exactly how to get a prefixed table name in Eloquent, but I bet there's an example somewhere in this repo, if not in the Entry model itself.