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.

Template Questions /

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

Control loop of Data Object not working


Go to End


2 Posts   3625 Views

Avatar
J4m3s

Community Member, 2 Posts

24 May 2011 at 1:57pm

Edited: 24/05/2011 1:58pm

Hi

I have set up a data object of testimonials that I want to display on the homepage of my site.

I've set it up so I add my testimonials on a testimonials tab when editing the homepage. It all seems to work fine, but I am having trouble displaying them through a control loop.

Quote.php

<?php
/**
 * Defines the Quote data object
 */
class Quote extends DataObject {
    
	static $db = array (
		'Quote' => 'Text',
		'Company' => 'Varchar(255)',
		'Name' => 'Varchar(255)'
	);
	
	function getCMSFields_forPopup() {
		$fields = new FieldSet();
		
		$fields->push( new TextField( 'Quote' ) );
		$fields->push( new TextField( 'Company' ) );
		$fields->push( new TextField( 'Name' ) );
		
		return $fields;     
		
	} 
	
    static $has_one = array (
		'HomePage' => 'HomePage'
	);			
  
}

Homepage.php

<?php
/**
 * Defines the HomePage page type
 */
 
class HomePage extends Page {
   
   static $db = array(
        'H1Heading' => 'Text',
		'BannerHeading' => 'Text',
		'BannerTxt' => 'HTMLText'
		    );
   
   static $has_one = array(
   );
   
   static $has_many = array (
		'Quotes' => 'Quote'
	);
   
  public function getCMSFields() {
        $fields = parent::getCMSFields();
         
        $fields->addFieldsToTab('Root.Content.Main', new TextField('H1Heading', 'H1 Heading'), 'Content');
		$fields->addFieldsToTab('Root.Content.Main', new TextField('BannerHeading', 'Banner Heading'), 'Content');
		$fields->addFieldsToTab('Root.Content.Main', new HtmlEditorField("BannerTxt","Banner Text"), 'Content');
		
		$tablefield = new HasManyComplexTableField(
			$this,
			'Quotes',
			'Quote',
			array(
				'Quote' => 'Quote','Company'=>'Company','Name' => 'Name'
				),
			'getCMSFields_forPopup'
			);
			
			$tablefield->setAddTitle( 'A Testimonial' );
			
			$fields->addFieldToTab( 'Root.Content.Quotes', $tablefield ); 
		
        return $fields;
    }
    
    
}
 
class HomePage_Controller extends Page_Controller {

     
}

HomePage.ss Control Loop

<% if Quotes %>
            	<% control Quotes %>
            	<div class="testimonials">
                    <p><span class="quoteStart">&#8220;</span>$Quote<span class="quoteEnd">&#8221;</span></p>
                    <div class="quoteSource"><p>$Name<strong>$Company</strong></p></div>
                </div>
            	<% end_control %> 
            <% else %>
                    No Quotes
            <% end_if %>

I'm new to Silverstripe and think I have got everything correct. If anyone could let me know what I might have wrong, that would be great.

Thanks in advance
James

Avatar
swaiba

Forum Moderator, 1899 Posts

24 May 2011 at 8:32pm

As always the exact "trouble" would be handy to know, I'll assume "No Quotes" is showing...

Firstly, read this for the debugging stuff (if you have not already), http://www.silverstripe.org/general-questions/show/16055

Secondly I would create a new controller function like this to test things are generally working...

function MyQuotes() {
return DataObject::get('Quote');
}

Then I'd try...

function MyQuotes() {
return DataObject::get('Quote','HomePageID='.$this->ID);
}

or

function MyQuotes() {
return $this->Quote;
}

I'd use Debug::show to check each of the functions is being called and returning the right values...