Jump to:

3450 Posts in 1063 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Nested URL problem when adding relations

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

Page: 1
Go to End
Author Topic: 1067 Views
  • neros3
    Avatar
    Community Member
    51 Posts

    Nested URL problem when adding relations Link to this post

    Hi

    I have a problem with the URLs when adding objects in relation.

    Class Car has_many Tires.

    $car->Tires()-add($tire)

    I can see that the ParentID in the database remains 0. I would expect the tires to end up under the car item in the CMS. But its just 0. Perhaps I misunderstood the ParentID. The relation however seems to work out fine.

    Thanks

  • neros3
    Avatar
    Community Member
    51 Posts

    Re: Nested URL problem when adding relations Link to this post

    AFAIK the menus are build from the ParentID, but the relations are made from the related IDs on each class/table.
    When adding a page to a related object the relation works fine but the ParentID stays 0.

    I have tried:

    1. Creating new object, saving with write() and then adding it to the Parent object.
    2. Creating new object, saving with write() and then adding it to the Parent object and then saving the newly created object againg.
    3. Creating new object, saving with write() and then adding it to the Parent object and then saving the newly created object againg and then saving the Parent object.

    But none of the above changes the ParentID. Am I missing something here?

    Thanks

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Nested URL problem when adding relations Link to this post

    you are talking about $car->Tires() and $car->Parent() which are two different things (at least on Pages). Tires() is a has_many relation as far as I can see. So you should have a has_one on the other side, if this has_one relation was called Parent then it would work however I expect you are dealing with pages, which already have a Parent field defined. To customize the parent of a page you would do $page->Parent = $someobject; or $page->ParentID = $someobject->ID; then call $page->write();

  • neros3
    Avatar
    Community Member
    51 Posts

    Re: Nested URL problem when adding relations Link to this post

    Hi !

    Thanks for your effort so far!

    The Car is defined with: has_many Tires.
    The Tire is defined with: has_one Car.

    I create a car ($car) and then add a tire like this:
    $tire = new Tire();
    $tire->Title = "my tire";
    $tire->URLSegment = "my-tire";
    $tire->write();

    $car->Tires()->add($tire);

    In the database the Tire table as a column called CarID - wich relates the tire to a specific car. When firing the above code the column CarID gets updated with the correct CarID. So that part works out just fine.

    Since its all Page types (extends Page) I would expect the new item (the tire) to end up as a children of the Car in the CMS. But since the ParentID in the SiteTree table for the tire row is 0 the new tire ends up in the root of the tree menu structure and not underneath the car as I would have expected.

    My concern is now: Am I doing something wrong here or am I supposed to manage ParentID's myself? I know I could just set the ParentID when i create the Tire, but for most of the cases in a CMS I think it would make sense to have that implemented by default.

    I hope this clears out my question.

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Nested URL problem when adding relations Link to this post

    Well yes like I said - $car->Tires() is another relation completely different to parent, children. 2 different things. So if you want to add the tires under the car then you would need to do ...

    $tire = new Tire();
    $tire->Title = "my tire";
    $tire->URLSegment = "my-tire";
    $tire->ParentID = $car->ID; // this says 'Tire is the Child of a Car'
    $tire->write();

  • neros3
    Avatar
    Community Member
    51 Posts

    Re: Nested URL problem when adding relations Link to this post

    ahh... ok! Then i'll stop pulling hair out of my head!

    Thanks!

    Btw. What's your comment on this? Wouldn't it make sense if this was taken care of by default? And is there an easy way to create a hook that works for all pages so that it by default would create the tree structure relation?

  • Dona J
    Avatar
    Community Member
    1 Post

    Re: Nested URL problem when adding relations Link to this post

    Such a very amazing link!
    Thanks you for the post.

    __________________
    Watch Predators Online Free

    1067 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.