Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Using ManyManyDataObjectManager on the belongs_many_many side

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2 3 4
Go to End
Author Topic: 5084 Views
  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Using ManyManyDataObjectManager on the belongs_many_many side Link to this post

    Hello.

    I have been trying to use the ManyManyDataObjectManager on both sides of the many-many relationship but when I try to load a page that has the belongs_many_many-relationship I get an empty javascript alert and then it says "Error loading the page". The ManyManyComplexTableField has the same behaviour so this is probably inherited from there.

    Isn't it supposed to work on both sides?
    I am trying for a setup where products and accessories are related to eachother. They are both pages so I want the user to be able to specify the relationship if they're creating a product or an accessory. Right now the only possible way to manage the relationship is through the product page which gets a bit clumsy.

    I will also be adding a third page to the mix later and then it will be even more important to be able to set the relationship from both sides.

    Kindly, Marcus

  • UncleCheese
    Avatar
    4085 Posts

    Re: Using ManyManyDataObjectManager on the belongs_many_many side Link to this post

    The belongs_many_many part of the relationship should be a descendant of DataObject, not SiteTree. So I'm not sure what you mean when you say you're looking at it from the other side, unless you're in ModelAdmin, maybe?

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Using ManyManyDataObjectManager on the belongs_many_many side Link to this post

    Does it really matter if it's a descendant of DataObject or not?
    Well of course it matters in some way since it doesn't work, but the other DataObjectManagers work on Sitetree and Sitetree inherits DataObject so Sitetree is a DataObject too.

    I have the relationships working now by using checkBoxSetField on the belongs_many_many-side. This solution was bugging before and that's why I tried using the ManyManyDataObjectManager on the belongs_many_many pages.

    To explain what I'm trying to do, I have two page types that are related to eachother, Products and Accessories. Products have the relationship $many_many and Accessories has the $belongs_many_many. The reason I tried this is because I want to be able to define the relationships when I make the Accessory, not just on the product side.

    Anyways, it's only the ManyManyDataObjectManager and it's ComplexField equivalent that throw the error, it actually kind of works if you use the hasManyDataObjectManager on the belongs_many_many-side. You can't actually manage the relationship but the DataObjectManager shows up and doesn't give an error.

    What I am thinking is that this is some kind of bug since you get an empty javascript dialog. Could you point me to the code that figures out what it's supposed to be fetching or is there some other way I can see what is happening and causing the error?

    I really think that the manymany-managers (yours and the original) should work if you're using it on the page that has $many_many or on the page that has the $belongs_many_many. After all, it should be pretty much the same functionality, the connection table is there after all and all that should have to be done is reverse it.

    The scenario I am working from here is that even though one is $many_many and the other one is $belongs_many_many the system needs to treat them more equal than in a has_one -> has_many. I pretty much have it working now with the checkBoxSetField but your DataObjectManager looks alot cooler ;)

  • UncleCheese
    Avatar
    4085 Posts

    Re: Using ManyManyDataObjectManager on the belongs_many_many side Link to this post

    I would start by just finding out what the error is. Turn on error reporting via email, look at the result of the AJAX request in Firebug. There are a number of things you can do to figure out what the error is. An empty alert box isn't much to go on.

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Using ManyManyDataObjectManager on the belongs_many_many side Link to this post

    Yes I will go digging and report back what I find!

  • Chris Dangerfield
    Avatar
    Community Member
    8 Posts

    Re: Using ManyManyDataObjectManager on the belongs_many_many side Link to this post

    Just wanting to know if you solved this one. I am having exactly the same difficulty.

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Using ManyManyDataObjectManager on the belongs_many_many side Link to this post

    No, I went for the checkBoxSetField solution since I was short on time and didn't have the time to troubleshoot it. Thanks for reminding me though, I actually have some spare time nowadays so I'll have a look at this and see if I can figure it out.

    I'll try to report back today with my findings.

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Using ManyManyDataObjectManager on the belongs_many_many side Link to this post

    Ok I have started tracing this.

    Fixing ManyManyComplexTableField is pretty simple. For some reason the code for figuring out which table to hit when it's a belongs_many_many is wrong. Simply replace the if statement that begins on line 28 or thereabouts with this:

    if( isset( $belongsManyManyRelations ) && array_key_exists( $this->name, $belongsManyManyRelations ) ) {
       $newSingleton = singleton($belongsManyManyRelations[$this->name]);
       $relationships = $newSingleton->uninherited('many_many', true);
       $tie = array_search($controller->ClassName, $relationships);
       $this->manyManyParentClass = $class;
       $manyManyTable = $belongsManyManyRelations[$this->name] . '_' . $tie;
       break;
    }


    This code will find the correct table to link up to. However ManyManyDataObjectManager seems to choke on some other error as well that occurs somewhere in the DataObjectManager base class. I am still tracking this error but the above code should tide you over with the ManyManyComplexTableField until I find and fix this.

    5084 Views
Page: 1 2 3 4
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.