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.

General Questions /

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

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

check for DataObjects on this page


Go to End


2 Posts   640 Views

Avatar
merrick_sd

Community Member, 99 Posts

23 November 2011 at 5:46am

I want to include a piece of custom javascript....But

I only want this to happen if BannerPage has items in the DataObject Banner.

$this->ShowBanner can successfully check on a field within BannerPage.

But i want to check if this BannerPage actually has images in the Dataobject called Banner

---banner.php dataobject --that holds field bannerimage
class Banner extends DataObject {

}

-- BannerPage --
class BannerPage extends Page {

public static $db = array(

'ShowBanner' => 'Boolean'
);

}

class BannerPage_Controller extends Page_Controller {

public function init() {
parent::init();

if($this->ShowBanner){
Requirements::customScript("

myjscode",bannerjs');

}else {

}

}//init

}//controller

I previously did <% if Banners %> in template but i want to now add js via the Controller instead

Maybe i need to add a function ??

public function CheckBanners() {
return DataObject::get("Banner", "PageID = {$this->ID}");
}

Avatar
Ryan M.

Community Member, 309 Posts

23 November 2011 at 1:55pm

I don't see any relationship between your BannerPage and the Banner object, such as has_one or has_many. But once you add a relationship, you can check for the existence of banners by doing this:

if($this->Banners()) {
//do something here
}