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.

Data Model Questions /

Nested URL problem when adding relations


Reply


7 Posts   1169 Views

Avatar
neros3

Community Member, 51 Posts

19 June 2010 at 11:03am

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

Avatar
neros3

Community Member, 51 Posts

19 June 2010 at 11:07pm

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

Avatar
Willr

Forum Moderator, 5513 Posts

20 June 2010 at 12:50am

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();

Avatar
neros3

Community Member, 51 Posts

20 June 2010 at 10:25pm

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.

Avatar
Willr

Forum Moderator, 5513 Posts

20 June 2010 at 10:36pm

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();

Avatar
neros3

Community Member, 51 Posts

20 June 2010 at 10:40pm

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?

Avatar
Dona J

Community Member, 1 Post

6 July 2010 at 2:42pm

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

__________________
[url=http://moviesonlinefree.biz]Watch Predators Online Free[/url]