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.

General Questions /

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

check for DataObjects on this page


Reply


2 Posts   394 Views

Avatar
merrick_sd

Community Member, 97 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
}