Hi All,
I am having problems with sorting data using a joined table.
The page is here: http://aneala.lochac.sca.org/people/show/321
I want to be able to sort on two fields from the jponed tables. I can do this on a single field with one using ->sort('field ...
but not using ->sort(array('field... which I need to add the second field for sorting.
Thanks for your help :)
This code works:
public function AwardList() {
// Combine the awards this person has received with the Award details for display plus the Precedence level to allow sorting.
return $this->Awards()
->leftJoin('Award', 'AwardReceived.AwardReceivedID = Award.ID')
->leftJoin('PrecedenceLevel', 'Award.PrecedenceID=PrecedenceLevel.ID')
->Sort('PrecedenceLevel.SortOrder ASC');
This doesn't:
public function AwardList() {
// Combine the awards this person has received with the Award details for display plus the Precedence level to allow sorting.
return $this->Awards()
->leftJoin('Award', 'AwardReceived.AwardReceivedID = Award.ID')
->leftJoin('PrecedenceLevel', 'Award.PrecedenceID=PrecedenceLevel.ID')
->Sort(array('PrecedenceLevel.SortOrder'=>'ASC'));
What I want to be able to do is:
public function AwardList() {
// Combine the awards this person has received with the Award details for display plus the Precedence level to allow sorting.
return $this->Awards()
->leftJoin('Award', 'AwardReceived.AwardReceivedID = Award.ID')
->leftJoin('PrecedenceLevel', 'Award.PrecedenceID=PrecedenceLevel.ID')
->Sort(array('PrecedenceLevel.SortOrder'=>'ASC', 'AwardReceived.ReceivedDate'=>'ASC'));
The pieces are:
class Person extends DataObject {
...
private static $has_many = array(
'Awards' => 'AwardReceived', //List of awards given to person
);
...
public function AwardList() {
// Combine the awards this person has received with the Award details for display plus the Precedence level to allow sorting.
return $this->Awards()
->leftJoin('Award', 'AwardReceived.AwardReceivedID = Award.ID')
->leftJoin('PrecedenceLevel', 'Award.PrecedenceID=PrecedenceLevel.ID')
->Sort(array('PrecedenceLevel.SortOrder'=>'ASC'));
}
class AwardReceived extends DataObject {
private static $db = array(
'ReceivedDate' => 'Date', //Date award given
);
private static $has_one = array(
'Person' => 'Person',//Link back to person receiving award
'AwardReceived' => 'Award', //Award Person has Received
'GivenBy' => 'Reign', //Link back to Reign for who gave out the award
'ReceivedEvent' => 'Event', //Link to the event where the award was given
);
class Award extends DataObject {
private static $db = array(
'SortOrder' => 'Int', //For display order in cms
'Name' => 'Varchar(100)', //Award Name
'Abbrev' => 'Varchar(20)', //Abbreviation
'Description' => 'HTMLText',
);
private static $has_one = array(
'TokenPic' => 'Image', //Image of award token
'Group' => 'SCAGroup', //Group this award belongs to
'Title' => 'Title', //Title conveyed by award. Contains both male and female honorific
'Precedence' => 'PrecedenceLevel', //Ranking amoungst other awards
'Parent' => 'Populace', //Link back to parent object
);