3093 Posts in 875 Topics by 654 members
| Go to End | Next > | |
| Author | Topic: | 2703 Views |
-
how to sort dataobjects with many-to-many relationship?

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'
);
} -
Re: how to sort dataobjects with many-to-many relationship?

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
-
Re: how to sort dataobjects with many-to-many relationship?

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. ^_^
-
Re: how to sort dataobjects with many-to-many relationship?

3 March 2010 at 9:46pm Last edited: 3 March 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 %> -
Re: how to sort dataobjects with many-to-many relationship?

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?
-
Re: how to sort dataobjects with many-to-many relationship?

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.
-
Re: how to sort dataobjects with many-to-many relationship?

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 ;) -
Re: how to sort dataobjects with many-to-many relationship?

4 March 2010 at 11:06pm
Yes, i thought as much.
Pretty useful
| 2703 Views | ||
| Go to Top | Next > |




