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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Sorting on a many_many


Go to End


4 Posts   1221 Views

Avatar
joelg

Community Member, 134 Posts

5 February 2015 at 9:08pm

Hi everybody,

I have a datamodel that looks pretty much like this:


class Lead extends DataObject{
    
    private static $db = array(
       ....
    );
    
    private static $many_many = array(
         'Types' => 'Type'
    );

}

class Type extends DataObject{
    
    private static $db = array(
        'Name' => 'Varchar(255)'
    );
    
    private static $belongs_many_many = array(
        'Leads' => 'Lead'
    );
}

Is it possible to do a sort on the Types?

Something like this?


Lead::get()->sort("Types.Name ASC");

Appreciate any help, thanks.

Joel

Avatar
martimiz

Forum Moderator, 1391 Posts

6 February 2015 at 4:44am

Could you give an idea of what exactly you ar looking for, maybe how the desired endresult would look, based on the example below?

Lead 1 -> Type C, Type A
Lead 2 -> Type A, Type C, Type B
Lead 3 -> Type B, Type A

Avatar
joelg

Community Member, 134 Posts

6 February 2015 at 9:04am

Hi Martimiz,

Thank you for writting.

Well something like this would be nice:

Lead 3 -> Type A
Lead 89-> Type A, Type B, Type A
Lead 4 -> Type A, Type B, Type C
Lead 22-> Type A, Type B, Type U
Lead 1-> Type B, Type C
Lead 2-> Type C, Type G
Lead 9-> Type K, Type L

I know it's horrible, but is it possible?

What's even more challenging is that I'm using it in a pagination...

Joel

Avatar
kinglozzer

Community Member, 187 Posts

6 February 2015 at 11:28pm

Hey Joel,

Could you reverse how you’re accessing the data to achieve this? E.g:

$leads = Type::get()->sort('Name')->relation('Leads');

Loz