Use of orderBy() clause in queries to order models
orderBy() has no effect
Use orderBy() in query and add different kind of orders, list returned is always the same. You can use the Random filter you will see that always the same list is returned.
First I was thinking of an error of mine as I was using it in a new plugin, but I've seem the same exact problem on a running website where I only want to change an orderBy() clause.
419
Looks to be working fine over here.
Db::table('users')->orderBy('name')->get();
Yes it works if you use DB::table(), but not if you use a model, like MyModel::orderBy('name')->get();
Again. Fine over here:
RainLab\User\Models\User::orderBy('name')->get();
This issue lacks enough detail or the replication instructions cannot be actioned easily. As per the contribution guidelines, please provide the exact steps you are taking for the issue to occur. This will be used for peer review.
Here is an example of a bug summary:
The CMS page URL is not being set automatically from the title value.
Here is an example of some reproduce steps:
- In the back-end, open the CMS > Pages section
- Click the "Add" button
- Enter [Hello] for the Title field
- Click the Save button
Here is an example of expected behavior:
- The Title should be set to [Hello]
- The URL should be set to [/hello]
Here is an example of actual behavior:
- The Title is set to [Hello] correctly
- The URL is left empty
I've tested with your code and it works.
Tested again with mine and still not ordering, like if orderBy() was ignored.
use \Tiipiik\Partners\Models\Partner;
function onStart() {
$this['partners'] = Partner::orderBy('name', 'desc')->get();
}
Maybe I missed something in the models but don't see what. I have the same problem on my Catalog plugin where I can't order products for example.
Try to use trace_sql() to find how SQL queries are built.
Maybe you are using NestedTree trait?
Please give us the full picture, we are doing blind guesswork. Paste your model class.
@jan-vince yes I use NestedTrait, but only in the Partners model, not in the Product one.
Here is the full model class
<?php namespace Tiipiik\Partners\Models;
use Model;
class Partner extends Model
{
use \October\Rain\Database\Traits\NestedTree;
public $table = 'tiipiik_partners_partners';
public $rules = [
'name' => 'required|unique:tiipiik_partners_partners',
'slug' => 'required',
];
public static $allowedSortingOptions = array(
'name asc' => 'Nom (ascendant)',
'name desc' => 'Nom (descendant)',
'random' => 'Al茅atoire',
'sort_order asc' => 'Ordonn茅 (ascendant)',
'sort_order desc' => 'Ordonn茅 (descendant)',
);
protected $guarded = ['*'];
protected $fillable = ['name', 'slug', 'description'];
public $attachOne = [
'cover' => ['System\Models\File', 'delete' => true]
];
}
use \October\Rain\Database\Traits\NestedTree;
Nested sets belongs to a strict structure defined by the user, they cannot be reordered at the query level, however you can order the collection they produce.
MyModel::get()->orderBy('name');
Or alternatively consider using \October\Rain\Database\Traits\SimpleTree combined with \October\Rain\Database\Traits\Sortable.
@jan-vince @daftspunk Yes, that's it !
I just tested by removing the NestedTree trait and it works, many thanks guys !