7913 Posts in 1355 Topics by 930 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » <% control Page() %> feature not working?
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
| Go to End | ||
| Author | Topic: | 2713 Views |
-
Re: <% control Page() %> feature not working?

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.
-
Re: <% control Page() %> feature not working?

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..
-
Re: <% control Page() %> feature not working?

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?
-
Re: <% control Page() %> feature not working?

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??
-
Re: <% control Page() %> feature not working?

23 December 2009 at 2:05pm
Hmmm. Odd indeed, i'm sure i remember a time when i used
and it returned true even though there were no items in the set. Something i should make sure i'm doing it right!if (DataObject::get('Page')) { .. }
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.
| 2713 Views | ||
| Go to Top |

