Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

SS3 Join Sort help


Go to End


920 Views

Avatar
mr3xcellent

Community Member, 2 Posts

18 April 2013 at 6:09am

Edited: 18/04/2013 6:13am

I'm trying to get a list of pages sorted by their Parent's Title.

I have two page types I'm working with here:

class AgencyPage extends Page {

public static $db = array(
'City' => 'Varchar',
'State' => 'Varchar',
'AgencyURL' => 'Varchar'
);

public static $has_one = array(
'Logo' => 'Image',
'AccountExec' => 'Member'
);

public static $has_many = array(
'Contacts' => 'Member.Agency'
);

AND

class ProjectPage extends Page
{
public static $db = array(
'Description' => 'Text',
'JobNumber' => 'Int',
'ProjectCode' => 'Varchar(6)',
'isArchived' => 'Boolean',
'ArchivedDate' => 'SS_Datetime'
);

public static $has_one = array(
'ArchivedBy' => 'Member',
'CreatedBy' => 'Member'
);

public static $has_many = array(
'ProjectFiles' => 'ProjectFile'
);

public static $many_many = array(
'ProjectContacts' => 'Member',
'ProjectViewers' => 'Member'
);

AgencyPage will be above ProjectPage in SiteTree (ProjectPage.ParentID = Agency.ID)

So, on the "All Projects" page, I list all the (current) projects. It's easy to sort my ProjectPage's by Title, Created, JobNumber, etc., but I'd like to allow the user to sort by the agency, or other 'joined' date, but can't seem to figure out how to get my DataList sorted that way.

What I'd like to be able to do is:

ProjectPage::get()->leftJoin('AgencyPage', 'ProjectPage.ParentID = AgencyPage.ID')->sort('AgencyPage.Title');

But this doesn't work as I get "Unknown column 'AgencyPage.Title' in 'field list'". This is because the joined table's columns aren't brought over, right?

I know I've seen similar questions before, but can't seem to relate their solutions to my situation.

What I'd like to know is what is the appropriate way to sort joined data within SS3? Any insight you kind folks can provide will be appreciated.

Thanks in advance,