Jump to:

3433 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » how to sort dataobjects with many-to-many relationship?

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

Page: 1 2
Go to End
Author Topic: 3365 Views
  • theAlien
    Avatar
    Community Member
    131 Posts

    how to sort dataobjects with many-to-many relationship? Link to this post

    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'
    );
    }

  • Mo
    Avatar
    Community Member
    506 Posts

    Re: how to sort dataobjects with many-to-many relationship? Link to this post

    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

  • Ben_W
    Avatar
    Community Member
    80 Posts

    Re: how to sort dataobjects with many-to-many relationship? Link to this post

    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. ^_^

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: how to sort dataobjects with many-to-many relationship? Link to this post

    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 %>

  • Mo
    Avatar
    Community Member
    506 Posts

    Re: how to sort dataobjects with many-to-many relationship? Link to this post

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

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: how to sort dataobjects with many-to-many relationship? Link to this post

    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.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: how to sort dataobjects with many-to-many relationship? Link to this post

    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 ;)

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: how to sort dataobjects with many-to-many relationship? Link to this post

    Yes, i thought as much.

    Pretty useful

    3365 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.