Jump to:

23489 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Checking for a dataobject that exists

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1 2
Go to End
Author Topic: 2499 Views
  • Harley
    Avatar
    Community Member
    153 Posts

    Checking for a dataobject that exists Link to this post

    Does anybody have any idea of how I can check whether or not a dataobject already exists? I'm just looking for an if statement that I can use before making my dataobject that I want to create.

    Thanks

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: Checking for a dataobject that exists Link to this post

    if(DataObject::get_one('DataObjectType'))

  • Harley
    Avatar
    Community Member
    153 Posts

    Re: Checking for a dataobject that exists Link to this post

    Thanks for the reply,

    I'm actually creating a group on the fly each time I create a page. The group is named by the title of the page so what I would like to do is check that there is not already a group of the same name.

    This is the if statement I am using but it doesn't seem to be working for me:

       if(DataObject::get_one('Group') != $this->Title)

    I'm sure it's just a sytax problem but I can't seem to figure it out

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Checking for a dataobject that exists Link to this post

    DataObject::get_one() returns the whole object so that comparison won't work. You can however add it as a where cause in the statement

    if(DataObject::get_one('Group', "Title != '$this->Title'"))

  • Harley
    Avatar
    Community Member
    153 Posts

    Re: Checking for a dataobject that exists Link to this post

    Thanks willr,

    I've tried your suggestion but it seems that this check still is not working, the security group is still being duplicated.

    To give you a better idea I have posted the code from my page class here:

       static $has_written = false;

       function onBeforeWrite(){

       if(DataObject::get_one('Group', "Title != '$this->Title'")){
          if(!self::$has_written){
                $group = new Group();
                $group->Title = $this->Title;
                $group->write();
             }
          self::$has_written = true;
       }
       parent::onBeforeWrite();
       }

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Checking for a dataobject that exists Link to this post

    Well think about what thats saying. Thats saying if there is not a Group without the Title 'Title'. Which will mean it won't work if you have any other group.

    Try something like

    if(!DataObject::get_one('Group', "Title = '$this->Title'"))

  • Harley
    Avatar
    Community Member
    153 Posts

    Re: Checking for a dataobject that exists Link to this post

    Ah you're the man!

    Yes that's it, works like a dream. One more question though related to this, when creating a new page I'm getting a security group being created for 'New Page'. Is there anyway to avoid this happening? I'm not quite sure why this is happening in the first place.

    Otherwise works like a charm now though

    Thanks again

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Checking for a dataobject that exists Link to this post

    if($this->Title != "New Page" && !DataObject::get_one('Group', "Title != '$this->Title'")) {

    Or is that just too hacky

    I'm not quite sure why this is happening in the first place.

    When you create a page it writes a copy of it (even before you have added any data) to ensure that the page ID exists, allowing you to add things like relationships without saving the page before hand.

    2499 Views
Page: 1 2
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.