Jump to:

22976 Posts in 11702 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » check for DataObjects on this page

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

Page: 1
Go to End
Author Topic: 301 Views
  • merrick_sd
    Avatar
    Community Member
    89 Posts

    check for DataObjects on this page Link to this post

    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}");   
    }

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: check for DataObjects on this page Link to this post

    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
    }

    301 Views
Page: 1
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.