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.