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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

[SOLVED] Testimonials Tutorial Code


Go to End


2 Posts   3790 Views

Avatar
Double-A-Ron

Community Member, 607 Posts

24 July 2009 at 10:26am

Edited: 24/07/2009 11:31am

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

Avatar
Double-A-Ron

Community Member, 607 Posts

24 July 2009 at 11:30am

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