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.

Data Model Questions /

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

how to sort dataobjects with many-to-many relationship?


Go to End


9 Posts   7188 Views

Avatar
theAlien

Community Member, 131 Posts

1 March 2010 at 11:40am

Hi,
I have a Many to Many relation between myPage and myDataObject.
In the frontend I would like to sort the output descending on date LastEdited (instead of the default ascending on ID).
I'm afraid I have to use joins, but I don have any experience with that...
Could someone help me out?

Some code of what I'm trying:

class myPage extends Page{
static $many_many = array (
'myDataObject' => 'myDataObject'
);
}

class myPage_Controller extends Page_Controller{
function MyObject(){
$myobject=DataObject::get('myDataObject',.......?.......,'LastEdited DESC');
return $myobject;
}}

class myDataObject extends DataObject{
static $belongs_many_many = array(
'myPage'=>'myPage'
);
}

Avatar
Mo

Community Member, 541 Posts

3 March 2010 at 2:03pm

For this instance, the join should be generated automatically. Not sure exactly what you are trying to do, but maybe you should add something like:

"myPageID = {$this->ID}"

This should add a filter to the returned myDataObject dataobjectset to only use dataobjects linked to the current page ID. Then you can use a control loop in your template to generate whatever html you want to display the data, EG:

<% if MyObject %>
    <ul>
        <% control MyObject %>
            <li>$YouObjectVariables</li>
        <% end_control %>
    </ul>
<% end_if %>

Is that the sort of thing you were after?

Mo

Avatar
Ben_W

Community Member, 80 Posts

3 March 2010 at 8:56pm

Mo is right, try add the line he had written 'myPageID={$this->ID}' into your query where you have hightlighted '...............?..............'
just remember that 'myPageID' is depend on how you declared the relationship in your dataobject class. In your code it is:

static $belongs_many_many = array(
'myPage'=>'myPage'
);

if your real code is

static $belongs_many_many = array(
'BelongToMyPage'=>'myPage'
);

then in your query it would have to be 'BelongToMyPageID={$this->ID}'

Hopefully I didn't make the matter worse. ^_^

Avatar
dhensby

Community Member, 253 Posts

3 March 2010 at 9:46pm

Edited: 03/03/2010 9:46pm

Surely the most simple solution to this is:

function getSortedmyDataObject() {
    return $this->myDataObject(null,'LastEdited DESC');
}

then in your template it is

<% control SortedmyDataObject %>
...
<% end_control %>

Avatar
Mo

Community Member, 541 Posts

4 March 2010 at 11:55am

Hmm, I wasn't aware you could do that. Would the getSortedmyDataObject() method return a dataobjectset of all linked dataobjects?

Avatar
dhensby

Community Member, 253 Posts

4 March 2010 at 1:11pm

Mo,

I believe it does return a DOS, but it might just be a component set, i'm not sure. either way, you can manipulate it as usual.

I've only used this functionality once. A little gem i discovered by just 'trying it out', not documented anywhere that i could find.

Avatar
bummzack

Community Member, 904 Posts

4 March 2010 at 7:45pm

Yes, you can pass WHERE, SORT, JOIN and LIMIT as parameters in relation getters.
AFAIK these actually map to the getComponents and getManyManyComponents methods in sapphire/core/model/DataObject.php. At least that's the place I look up the parameter order when I forget it ;)

Avatar
dhensby

Community Member, 253 Posts

4 March 2010 at 11:06pm

Yes, i thought as much.

Pretty useful

Go to Top