Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Problem with Relationships for DataObject on Page.php

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

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

    Problem with Relationships for DataObject on Page.php Link to this post

    Hi guys, me again! Looking for a bit of help as I've tied myself in knots here!

    I'm looking to create a new tab in the CMS with a ComplexTableField, where the user can add Testimonials that relate to each specific page. (This will be for ANY page on the site that uses or derives from "Page.php", but each testimonial will be unique to only the page it was created on.)

    Initially I had set it up so that there was only one testimonial per page; on page.php I used the static $db array to create a few fields (quote, name, company)

    However, we now want to have it so that the user can add as many Testimonials as they like. I decided to do this using the ComplexTableField.

    First of all I created the object, Testimonial.php:

    <?php
    class Testimonial extends DataObject {
       static $db = array(
          'Quote' => 'Text',
          'Name' => 'Varchar(255)',
          'Company' => 'Varchar(255)',
          'Status' => "Enum('Active,Inactive', 'Active')",
       );
       
       static $singular_name = 'Testimonial';
       static $plural_name = 'Testimonials';
       
       static $has_one = array(
       );
       
       static $default_sort = "Name ASC";
    }
    ?>

    Then in page.php I put in the following code:

    ...
       public static $has_one = array(
          'Testimonials' => 'Testimonial',
       );

       function getCMSFields() {
          $fields = parent::getCMSFields();
          $TestimonialsTable = new ComplexTableField($this, 'Testimonials', 'Testimonial', array('Name'=> 'Name'));
          
          $fields->addFieldToTab("Root.Content.Testimonial", new HeaderField( Client Testimonials:', '3', true ));
          $fields->addFieldToTab('Root.Content.Testimonial', $TestimonialsTable);
          
          return $fields;
       }
    ...

    So this *seemed* to work, but then I realised that if I add a testimonial on one page, it appears on ALL pages that derive from page.php!!!

    I guess I've got the relationship thing wrong somewhere?

    I tried adding the following code to Testimonial.php, but it didn't have the desired effect - it just created a dropdown box with all the site pages in the modal dialog! (So I've removed it again)

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

    Basically, I need to know how to say "when I create a testimonial on Page-A it only appears on Page-A"

    All help is appreciated, as always!!! I'm reading through my silverstripe book just now to try and figure it out, but my brain hurts!!!

    Thanks,

    Linsey (",)

  • Juanitou
    Avatar
    Community Member
    323 Posts

    Re: Problem with Relationships for DataObject on Page.php Link to this post

    Hi!

    What your template looks like? Have you verified in the database that Pages you create have a TestimonialID column?

    Have you tried with HasOneComplexTableField? There is a one-to-one switch. Some reading: http://doc.silverstripe.org/doku.php?id=HasOneComplexTableField

    Hope it helps,
    Juan

  • LinseyM
    Avatar
    Community Member
    99 Posts

    Re: Problem with Relationships for DataObject on Page.php Link to this post

    Hi there,

    I'll try this HasOneComplexTableField.

    I think I'm missing the point somewhere though, in terms of the relationship thing. Confused by the fact that if a put a new field in via the DB array, then it only appears on the page where I added it, but the compex table items seem to appear on every page!

    I'll let you know how I get on with the HasOneComplexTableField, and thanks for helping me again! Twice in a week!

    (",)

  • Juanitou
    Avatar
    Community Member
    323 Posts

    Re: Problem with Relationships for DataObject on Page.php Link to this post

    Hi Linsey,

    I wonder if you are talking about the CMS or the front site when you say: ‘but the complex table items seem to appear on every page!’. That’s why I asked you about your template. In any case, if you want a real 1-to-1, I think you need a HasOneComplexTableField. Also, don’t forget about UncleCheese’s DataObjectManager, which implements similar fields.

    Regards,
    Juan

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