Jump to:

3444 Posts in 1030 Topics by 871 members

Template Questions

SilverStripe Forums » Template Questions » Control loop of Data Object not working

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

Page: 1
Go to End
Author Topic: 1494 Views
  • J4m3s
    Avatar
    Community Member
    2 Posts

    Control loop of Data Object not working Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Control loop of Data Object not working Link to this post

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

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