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

Two different many_many relationships between the same objects


Go to End


3 Posts   1985 Views

Avatar
cm

Community Member, 7 Posts

1 September 2010 at 3:47am

Hi,

What I'd like to do is have to different relationships between the same objects. For example, a user can have two relationships to a page, favorite and/or superfavorite.

I have it set up as follows:

SomePage.php
static $belongs_many_many = array(
'Superfavorite'=>'User',
'Favorites'=>'User'
);

User.php
static $many_many = array(
'Superfavorite' => 'SomePage',
'Favorites' => 'SomePage'
);

When I call it from the users end to check the relationship or add/remove, it works just fine, I can do:
$user->Superfavorite()->getIdList();
or
$user->Favorites()->getIdList();
and get the expected response.

But when I do it from the Page end (e.g. $somepage->Favorites()->getIdList();), it only ever gives me the results from the table that shows up first in the arrays in SomePage.php and User.php.

Is it a bug or is there another way I should be doing this?

Thanks!

Avatar
Willr

Forum Moderator, 5523 Posts

1 September 2010 at 6:04pm

I know at least has_many's support dot notation for differing between relations to the same classes. You could try that with many_many (no idea if works though).

http://doc.silverstripe.org/datamodel#has_many

Avatar
cm

Community Member, 7 Posts

2 September 2010 at 4:25am

Thanks. I finally found this, also: http://open.silverstripe.org/ticket/4546

Which I think is the same problem that I'm having. I'll try a couple of things to work around it.