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

Issue with DataObject:get and ParentID


Reply

4 Posts   6563 Views

Avatar
Chris Rae

16 January 2009 at 4:47pm (Last edited: 16 January 2009 4:48pm), Community Member, 17 Posts

Hey folks!

I found a great code snippet here written by Sam which I have pinched and am trying to modify slightly:
http://www.silverstripe.org/archive/show/117318

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!

EG:
$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.

Avatar
Chris Rae

18 January 2009 at 12:28pm Community Member, 17 Posts

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");
$items->toArray();

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

Avatar
Chris Rae

18 January 2009 at 2:09pm Community Member, 17 Posts

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

Avatar
dacar

25 February 2010 at 5:00am Community Member, 168 Posts

hi chrisrae,

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

Carsten.