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.

Data Model Questions

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


Go to End
Reply

9 Posts   3457 Views

Avatar
theAlien

1 March 2010 at 11:40am Community Member, 131 Posts

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

3 March 2010 at 2:03pm Community Member, 509 Posts

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

3 March 2010 at 8:56pm Community Member, 80 Posts

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
Pigeon

3 March 2010 at 9:46pm (Last edited: 3 March 2010 9:46pm), Community Member, 243 Posts

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

4 March 2010 at 11:55am Community Member, 509 Posts

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

Avatar
Pigeon

4 March 2010 at 1:11pm Community Member, 243 Posts

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
banal

4 March 2010 at 7:45pm Community Member, 901 Posts

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
Pigeon

4 March 2010 at 11:06pm Community Member, 243 Posts

Yes, i thought as much.

Pretty useful

Go to Top