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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Data Model Questions /

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

Issue with DataObject:get and ParentID

Go to End

4 Posts   7598 Views

Chris Rae

Community Member, 17 Posts

16 January 2009 at 4:47pm

Edited: 16/01/2009 4:48pm

Hey folks!

I found a great code snippet here written by Sam which I have pinched and am trying to modify slightly:

Here's what I'm doing with it:

$url = $this->ParentURLSegment;
$parent = DataObject::get_one('SiteTree', "`URLSegment` = '$url'");
return DataObject::get("SiteTree", "ParentID = $parent->ID", "", "", "3");

$url is a Varchar and I set it in the CMS in a text field, it has a default value of "home".

The problem is that when I use $url to define my URLSegment, I have a strange issue with ParentID:
Silverstripe tries to execute SQL queries containing this:
WHERE (ParentID = )
i.e the ParentID is nothing.

I can echo $url and check it in the database itself, and it is the URLSegment I want it to be (home).

When I use a straight string instead of $url - it works perfectly!

$url = $this->ParentURLSegment;
$parent = DataObject::get_one('SiteTree', "`URLSegment` = 'home'");
return DataObject::get("SiteTree", "ParentID = $parent->ID", "", "", "3");

When the code gets to:

return DataObject::get("SiteTree", "ParentID = $parent->ID", "", "", "3");

Using $url $parent->ID is nothing (EG WHERE (ParentID = )

Using "home" $parent->ID is correct (EG WHERE (ParentID = 3124)

I hope that is clear enough, its driving me up the wall! :)

I'm very happy to provide any additional information.

Thanks guys, I appreciate any help.

Chris Rae

Community Member, 17 Posts

18 January 2009 at 12:28pm

There was nothing wrong with the code, it fixed itself after a reboot..

I have another issue though :D

$items isn't an object? (I thought DataObject::get returns a DataObjectSet?)

$parent = DataObject::get_one('SiteTree', "`URLSegment` = '$url'");
$items = DataObject::get("SiteTree", "ParentID = $parent->ID", "", "", "4");

Whenever I try to apply any method applicable to a DataObjectSet ($items->First, ->Last(), ->Count() etc etc) I get a:
Call to a member function toArray() on a non-object error.

By assigning my get to $items - what have I done that was so bad?

I can't do this either: (I was trying to get around having to assign the returned data to a var)
DataObject::get("SiteTree", "ParentID = $parent->ID", "", "", "4")->Count();

Any takers?

Thanks guys

Chris Rae

Community Member, 17 Posts

18 January 2009 at 2:09pm

aj sorted my ridiculous questions out via IRC, much appreciated, sorry for the bother ;)


Community Member, 173 Posts

25 February 2010 at 5:00am

hi chrisrae,

can you please explain how you solved the DataObject::get toArray() problem?