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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

<% control Page() %> feature not working?


Go to End


13 Posts   5846 Views

Avatar
dhensby

Community Member, 253 Posts

21 December 2009 at 1:46pm

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.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

22 December 2009 at 3:32am

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


Avatar
dhensby

Community Member, 253 Posts

22 December 2009 at 2:04pm

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

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

Avatar
UncleCheese

Forum Moderator, 4102 Posts

23 December 2009 at 3:10am

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??

Avatar
dhensby

Community Member, 253 Posts

23 December 2009 at 2:05pm

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.

Go to Top