Jump to:

3429 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » How do I set up this relationship / data object?

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

Page: 1
Go to End
Author Topic: 1082 Views
  • LinseyM
    Avatar
    Community Member
    99 Posts

    How do I set up this relationship / data object? Link to this post

    Hi there guys,

    I've been reading through the Job Postings tutorial in the SS book, but I've tied myself in knots, so I thought I would just ask instead, as I am beyond confused now. (This vaguely relates to a previous post, but am starting over as the client changed their mind on the last part of the project anyway!)

    OK, I want to created a data object. Let's call it "LinkedPDF"

    The basic idea is that from any "service page"* on the website CMS I have a tab called "Related PDFs". (*these pages all use the template and page type "ServicePage")

    In this "Related PDFs" tab I will show a ComplexTableField. This will allow users to add links to related PDF files to the web page (plus some notes / text / graphic). The plan is that they can add as many Related PDF items as they want, so no point hard-coding 5 links in the static $db for example. Also, I want to do it this way rather than asking them just to create web links on the page as they want it laid out / formatted in a way that would be too tricky for them to achieve in the CMS.

    Anyway, I followed the Job Posting tutorial and got the ComplexTableField working. Thought all was well, and added a few linked case studies for "Service Page A", but then i realised that these case studies now appeared linked to ALL Pages that used the "ServicePage" template / page type! DOH!

    So, i kinda know where I've gone wrong... the data objects are linked to the page type and not the actual page i created them from.

    It's not practical to create a new "Holder" for each service page - there are too many - so how can I fix this?

    Hope I've got all the techie terminology correct re dataObjects, relationships etc...!

    All answers on a postcard please - any help much appreciated.

    Thanks,

    Linsey

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: How do I set up this relationship / data object? Link to this post

    Hi Lindsey!

    First of all, I would really really recommend that you get the DataObject Manager. If you're running on 2.4 then get the trunk version, otherwise get the 2.3 branch. You'll also need the SWFUpload module (can be found here).

    DataObject Manager is essentially a very much improved version of ComplexTableField so you use that instead of ComplexTableField.
    There is a special version of DataObject Manager called FileDataObjectManager and it's perfect for the scenario that you are describing (if I'm understanding you correctly).

    When it comes to the ServicePage and the LinkedPDF, make sure that ServicePage $has_many LinkedPDF and LinkedPDF $has_one ServicePage so that the relationship is setup properly.

    ServicePage

    public static $has_many = array(
       'LinkedPDFs'    => 'LinkedPDF'
    );

    LinkedPDF

    static $has_one = array (
       'Attachment' => 'File',
       'ServicePage' => 'ServicePage'
    );

    Now as you might have noticed the LinkedPDF is not a file in itself but a DataObject linked to a file (this might be the way you've done it already, I'm not sure). You can have whatever other fields you want on the LinkedPDF object according to your needs.

    Last but not least you add the FileDataObjectManager in the getCMSFields() method like so

    ServicePage

    public function getCMSFields() {
       $fields = parent::getCMSFields();
       $manager = new FileDataObjectManager(
          $this, // Controller
          'LinkedPDFs', // Source name
          'LinkedPDF', // Source class
          'Attachment', // File name on DataObject
          array(), // Headings
       );
    $fields->addFieldToTab("Root.Content.LinkedPDFs", $manager);

       return $fields;
    }

    With this code you should be able to start linking your files to your ServicePages. I've also included the basics from the code I use when my clients need to have the ability to link an arbitrary number of files to any page in the system.

    Kindly,
    Marcus

    Attached Files
  • LinseyM
    Avatar
    Community Member
    99 Posts

    Re: How do I set up this relationship / data object? Link to this post

    Hi marcus,

    Thanks so much for this detailed reply.

    I'll have a look at the version requirements and read up on the DataObjectManager.

    Will give it all a try tomorrow morning when I have a clear head - its too late in the day for anything too complicated now!

    Many many thanks!

    Linsey

    1082 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.