Jump to:

3430 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Issue with DataObject:get and ParentID

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

Page: 1
Go to End
Author Topic: 6485 Views
  • Chris Rae
    Avatar
    Community Member
    17 Posts

    Issue with DataObject:get and ParentID Link to this post

    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.

  • Chris Rae
    Avatar
    Community Member
    17 Posts

    Re: Issue with DataObject:get and ParentID Link to this post

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

    I have another issue though

    $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

  • Chris Rae
    Avatar
    Community Member
    17 Posts

    Re: Issue with DataObject:get and ParentID Link to this post

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

  • dacar
    Avatar
    Community Member
    168 Posts

    Re: Issue with DataObject:get and ParentID Link to this post

    hi chrisrae,

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

    Carsten.

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