Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » <% control Page() %> feature not working?

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2
Go to End
Author Topic: 3418 Views
  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: <% control Page() %> feature not working? Link to this post

    Yes, i completely understand.

    I never use URLSegments to identify pages or for use in logic, its just crazy and isn't really in keeping with the MVC model.

    But perhaps there is a better way to list the pages than the last way... I'm still not sure what you are trying to do, but if it works as you want, then i am glad

    Small things like that really require a bit of a solid understanding of SS to get done quickly, so i'm happy to help

    Good luck with your site.

  • UncleCheese
    Avatar
    4085 Posts

    Re: <% control Page() %> feature not working? Link to this post

    Yeah, Pigeon, you're right. URLSegments can easily change due to user error in the CMS, and it's bad practice to rely on them. Most of the time this happens, you're trying to control a page that has only one instance and will only ever have one instance. Holder pages are a classic example. If you only have one news section on your site, then NewsHolder is only created once and you may need to access that page from other templates. That's why I usually create a special page class for that.

    class SinglePage extends Page
    {
    public static function instance() { return DataObject::get_one(__CLASS__);

    public function canCreate() {return !self::instance() }

    }

    class NewsHolder extends SinglePage
    {

    ...

    }

    This does two things.. 1) Guarantees there is only one NewsHolder page can only be represented once in the CMS. Can't have two newsholders simultaneously. and 2) You can easily get the NewsHolder instance with:

    NewsHolder::instance();

    NewsHolder::instance()->Link();
    NewsHolder::instalce()->ID;

    etc..

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: <% control Page() %> feature not working? Link to this post

    Clever, i like that solution. And handy to be able to get an instance so easily!

    Although, isn't returning self::instance()->exisits() better?

  • UncleCheese
    Avatar
    4085 Posts

    Re: <% control Page() %> feature not working? Link to this post

    Well, no, because DataObject::get() returns false if no record is matched, so if you daisy chain the exists() method, you'll get a non-object error. I never really got what the exists() method does. I think it's only for DataObjectSet objects to count the number of contained items as more than zero.

    But DataObject::get_one() does not return a DOS, so if that's the case then it won't work. If you can run exists() on a DataObject alone, then I suppose the best evaluation is:

    return self::instance() && self::instance()->exists();

    But again, I don't know of any scenario in which get_one() would return true, but exists() return false??

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: <% control Page() %> feature not working? Link to this post

    Hmmm. Odd indeed, i'm sure i remember a time when i used

    if (DataObject::get('Page')) { .. }

    and it returned true even though there were no items in the set. Something i should make sure i'm doing it right!

    I'm sure exisits would never return true if the get_one returned false, it's just the case of there being a Set, but no actual objects that is the issue. SS should make that consistent so one or the other is used. If the DataObject::get.. methods always returned something and exists() was consistently used to verify returned objects, then it would be much more straight forward.

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