Jump to content

sorting by subfields


valan
 Share

Recommended Posts

Does 'sort' support subfields?

I'm trying to sort pages like this:

$query = 'template=mytemplate,sort=myfield.mysubfield';

$result  = wire('pages')->find($query);

It generates:

Exception: SQLSTATE[42S22]: Column not found: 1054 Unknown column '_sort_myfield.mysubfield' in 'mytemplate clause' (in var/www/pw/wire/core/PageFinder.php line 298)

E.g. is there any way to sort by subfields in a query?

Link to comment
Share on other sites

Is this subfield stored in the db or just runtime generated?

This subfield is stored in db.

When you're not using limit in the selector you could try to sort the result in memory after getting it from the db. 
 
Do you mean that this should work? Not sure but will give a try.
$query = 'template=mytemplate';
$result  = wire('pages')->find($query)->sort('myfield.mysubfield');
Link to comment
Share on other sites

Andrey,

sort by the real name of the database column (in your case it is 'data') and it will work, i.e.

$query = 'template=mytemplate,sort=myfield.data';//data here = real name of the mysubfield, not its alias

At the moment, I don't know how to tell sort to sort by the alias of a subfield...Searching using an alias (if set) works fine though, e.g. 

$p = $pages->get('template=mytemplate, myfield.mysubfield=whatever');
Edited by kongondo
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...