Jump to:

3428 Posts in 1056 Topics by 733 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Two different many_many relationships between the same objects

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

Page: 1
Go to End
Author Topic: 1101 Views
  • cm
    Avatar
    Community Member
    7 Posts

    Two different many_many relationships between the same objects Link to this post

    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!

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: Two different many_many relationships between the same objects Link to this post

    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

  • cm
    Avatar
    Community Member
    7 Posts

    Re: Two different many_many relationships between the same objects Link to this post

    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.

    1101 Views
Page: 1
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.