Jump to:

23368 Posts in 18143 Topics by 2863 members

General Questions

SilverStripe Forums » General Questions » DataObjects Per Page Instance

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 2
Go to End
Author Topic: 1966 Views
  • Lazarus404
    Avatar
    Community Member
    71 Posts

    DataObjects Per Page Instance Link to this post

    Hey guys,

    I have a DataObject :-

    class PanelContent extends DataObject
    {
       public static $TopID = 1;
       
       static $db = array(
          'Title' => 'Varchar',
          'Group' => 'Varchar',
          'Content' => 'HTMLText'
       );

       static $has_one = array(
          'Image' => 'Image',
          'Page' => 'Page'
       );

       function getCMSFields_forPopup()
       {
          $fields = new FieldSet();
          $fields->push( new TextField( 'Title' ) );
          $fields->push( new TextField( 'Group' ) );
          $fields->push( new SimpleHTMLEditorField( 'Content' ) );
          $fields->push( new ImageField( 'Image', 'Header Image', null, null, null, 'Uploads' ) );
          return $fields;
       }
    }

    This object acts as a panel in a given template

    My page object then links them like this :-

    public static $has_many = array(
       "PanelItems" => "PanelContent"
    );

    This works great for one page, but the problem is, if I create more instances of this page, they "share" those objects. I need the objects to be bespoke for each page. How can I do this?

    Thanks loads,
    Lee

  • Lazarus404
    Avatar
    Community Member
    71 Posts

    Re: DataObjects Per Page Instance Link to this post

    Anyone?

  • Gelert
    Avatar
    Community Member
    8 Posts

    Re: DataObjects Per Page Instance Link to this post

    Try following this tutorial:
    http://www.ssbits.com/tutorials/2010/dataobjects-as-pages-part-2-using-model-admin-and-url-segments-to-create-a-product-catalogue/

    Or another similar one on that site.

    I've used them to solve what you're having difficulty with.

    I think you need a PageHolder to hold the Data Objects from looking at your code.

    Good luck!

  • Lazarus404
    Avatar
    Community Member
    71 Posts

    Re: DataObjects Per Page Instance Link to this post

    Thank you, but this doesn't cover my predicament. I need to have it so no dataobjects from other pages (with the same template) are visible in each other page using the same template. So, in my circumstance, I have a bio's page and a clients page. Both pages use the same dataobject types, and the same page template, but both should have only their own data. They shouldn't cross pollute.

    Thanks,
    Lee

  • Gelert
    Avatar
    Community Member
    8 Posts

    Re: DataObjects Per Page Instance Link to this post

    I see. You need to filter your Dataobjects on something which is unique to each page.

    So add a Enumerated data item called "DisplayOn" containing "bio" and "clients" and assign each Data Object a DisplayOn.

    "DisplayOn" => "Enum('bio, clients')"

    Then in your page controller where you want to display create two functions, one to filter one way and one to filter the other. Then you won't get Data Objects appearing on the wrong page.

    <?php

       function BioObjects() {
          // set filters
          $filterSQL=" 1=1 ";
          $filterSQL .= " AND DisplayOn = 'bio'";         
          
          $BioObjects = DataObject::get(
             "PanelContent",
             $filterSQL, // filter
             null, // sort
             null, // join
             null // limit
          );
          
          return $BioObjects;
          exit;
       } // end function

    ?>

    /// Template

       <% if BioObjects %>
       <% control BioObjects %>
             <h1>$Title</h1>
       <% end_control %>
       <% end_if %>

    <?php

       function ClientObjects() {
          // set filters
          $filterSQL=" 1=1 ";
          $filterSQL .= " AND DisplayOn = 'clients'";         
          
          $ClientObjects = DataObject::get(
             "PanelContent",
             $filterSQL, // filter
             null, // sort
             null, // join
             null // limit
          );
          
          return $ClientObjects;
          exit;
       } // end function

    ?>

    /// Template 2

       <% if ClientObjects %>
       <% control ClientObjects %>
             <h1>$Title</h1>
       <% end_control %>
       <% end_if %>

    You can be more sophisticated and use a ?filter=bio and ?filter=clients and grab that from the URL if you need. But I'd just make two PageTypes and hard-wire the filter in this situation.

    Best
    Aled

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: DataObjects Per Page Instance Link to this post

    The way you set this up looks OK. Your DataObject will have a PageID field, that should hold the link to the correct page (it will be created based on the has_one relation to the page you defined), but all depends on how you add your DataObjects to an individual Page.

    Are you using something like a ComplexTableField or even a DataObjectManager on your Page? In that case the PageID field should be set automatically, you could check your database table ..

    If the PageID is set correctly, then the simplest way of displaying one or more DataObjects on a given page is something like this in your Page.ss template:

    <div id='somePanel'>
    <% control PanelItems %>
    $Title <br />
    $Group <br />
    $Content
    <% end_control %>
    </div>

    There are lots of different setups thinkable, but you'll always need the PageID present in your DataObjects.

  • Lazarus404
    Avatar
    Community Member
    71 Posts

    Re: DataObjects Per Page Instance Link to this post

    Hi Martimiz,

    I figured there must be an easier way because of that link. In my PanelPage class, I'm setting up the CMS fields like this:

    function getCMSFields() {
          $fields = parent::getCMSFields();
          $contentTablefield = new HasManyDataObjectManager(
             $this,
             'PanelItems',
             'PanelContent',
             array(
                'Title' => 'Title',
                'Group' => 'Group Name',
                'Content' => 'Content'
             ),
             'getCMSFields_forPopup'
          );
          $contentTablefield->setAddTitle( 'Content Items' );

          $fields->addFieldToTab( 'Root.Content.ContentPanels', $contentTablefield );

          return $fields;
    }

    In the CMS, both pages show all PanelItems, where I really only want to show the items specific to the page.

    Thanks for your time.

    Lee

    PS @Gelert, thanks for your time... I really appreciate that, too

  • Lazarus404
    Avatar
    Community Member
    71 Posts

    Re: DataObjects Per Page Instance Link to this post

    Oh, and yes, the PageID is set for these objects

    Thanks,
    Lee

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