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.

Data Model Questions /

Problem with Relationships for DataObject on Page.php


Reply


4 Posts   1849 Views

Avatar
LinseyM

Community Member, 99 Posts

10 March 2010 at 11:53am

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 (",)

Avatar
Juanitou

Community Member, 323 Posts

11 March 2010 at 2:50am

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

Avatar
LinseyM

Community Member, 99 Posts

11 March 2010 at 2:40pm

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!

(",)

Avatar
Juanitou

Community Member, 323 Posts

12 March 2010 at 2:49am

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