Jump to:

7940 Posts in 1543 Topics by 946 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » [SOLVED] Testimonials Tutorial Code

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 2521 Views
  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    [SOLVED] Testimonials Tutorial Code Link to this post

    Hi all. I have a client in need of a Testimonials section of their site and decided to have a gander at the sample code at the top of http://doc.silverstripe.com/doku.php?id=modules:dataobjectmanager.

    I had a few issues, and am having continuing issues still. It's strange. At first it seemed to work, but no more.

    First, here's the exact code I am using:

    TestimonialPage.php

    class TestimonialPage extends Page {
       
       public static $db = array(
       );
       
       public static $has_one = array(
       );
       
       static $has_many = array(
          'Testimonials' => 'Testimonial'
       );
       
       static $icon = "mysite/images/treeicons/testimonials";   
       
       static $defaults = array(
          'ShowInMenus' => false
       );
       
       public function getCMSFields()
       {
          $fields = parent::getCMSFields();

             $fields->addFieldToTab("Root.Content.Testimonials", new DataObjectManager(
                $this,
                'Testimonials',
                'Testimonial',
                array('Date' => 'Date','Author'=>'Author','Quote' => 'Quote'),
                'getCMSFields_forPopup'
             ));
          
          return $fields;
          
       }
       
    }

    Testimonial.php

    class Testimonial extends DataObject
    {
       static $db = array (
          'Date' => 'Date',
          'Author' => 'Text',
          'Quote' => 'HTMLText'
       );
       
       static $has_one = array (
          'TestimonialPage' => 'TestimonialPage'
       );
          
       public function getCMSFields_forPopup()
       {
          return new FieldSet(
             new CalendarDateField('Date'),
             new TextField('Author'),
             new TextareaField('Quote')
          );
       }
    }

    Problem 1: At first this all worked. But when I tried to get one Testimonial in the template, I had the errors as seen in this thread: http://www.silverstripe.org/data-model-questions/show/265300#post265300.

    Problem 2: Following the above, I completely rewrote both classes just using my noodle and old code (that I know works) as a guide. After doing this, trying to load an existing TestimonialPage in the CMS shows a blank page on the right. So I refresh, entire CMS crashes without error. I managed to isolate the location of this problem to the $fields->addFieldToTab("Root.Content.Testimonials", new DataObjectManager( block in TestimonialPage.php. If I comment this out, no problem, but of course, the table and DataObjectManager are missing.

    Something must be wrong with my code, as I am using DataObjectManager for the image gallery without issue.

    Can someone check my code and let me know what I've bollixed please? I've gone over it such a simple peice so many times now.

    Cheers
    Aaron

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: [SOLVED] Testimonials Tutorial Code Link to this post

    Cancel that. I completely rewrote the classes for a third time and tested in a sandbox. All working now so there must have been a minor error in my original code.

    FYI, here is the working code for the Testimonials that I am using:

    TestimonialPage.php

    class TestimonialPage extends Page {
       
       static $db = array(
       );
       
       static $has_one = array(
       );

    static $has_many = array(
    'Testimonials' => 'Testimonial'
    );

       static $icon = "mysite/images/treeicons/testimonials";

    static $defaults = array(
    'ShowInMenus' => false
    );
       
       function getCMSFields() {
          $fields = parent::getCMSFields();

    $fields->addFieldToTab("Root.Content.Testimonials", new DataObjectManager(
    $this,
    'Testimonials',
    'Testimonial',
    array('Date' => 'Date','Author'=>'Author','Quote' => 'Quote'),
    'getCMSFields_forPopup'
    ));

          return $fields;
       }
       
    }

    Testimonial.php

    class Testimonial extends DataObject {

    static $db = array(
    'Date' => 'Date',
    'Author' => 'Text',
    'Quote' => 'HTMLText'
    );

    static $has_one = array (
    'TestimonialPage' => 'TestimonialPage'
    );

    function getCMSFields_forPopup() {
    return new FieldSet(
    new CalendarDateField('Date'),
    new TextField('Author'),
    new TextareaField('Quote')
    );
    }
    }

    Nothing else needed to change, and both issues are fine now.

    Aaron

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