Jump to:

23001 Posts in 11854 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » data object loop inside another loop not working

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 457 Views
  • sajok
    Avatar
    Community Member
    82 Posts

    data object loop inside another loop not working Link to this post

    Hello,

    I have a page type "CentersPage" that shows centers in an accordion style. This page type has a "has_many" relationship with two data objects "Center" and "Location". Each accordion option has title (location) that when clicked on shows a table of centers that belong to that location.

    <?php
    class CentersPage extends Page {

       static $has_many = array(
          'Locations' => 'Location',
          'Centers' => 'Center'
       );

    }
    class CentersPage_Controller extends Page_Controller {
    function centerLocations () {
    $Locations = DataList::create('Location');
    if ($Locations) {
    foreach ($Locations as $Location) {
       $dosSubSet = DataList::create('Center')->filter('LocationID', $Location->ID);
    $Location->Centers = $dosSubSet;
    }
    }
    return $Locations;
    }

    }

    <?php

    class Center extends DataObject {
    static $db = array(
    'Title' => 'Varchar',
    'Description' => 'Text'
    )
    static $has_one = array(
    'Page' => 'CentersPage',
    'Location' => 'Location'
    );
    }

    <?php

    class Location extends DataObject {
    public static $db = array(
       'Title' => 'Varchar'
    );
    }

    and in the template CentersPage.ss I have the following code:

       <div id="accordion">
             <% if centerLocations %>
                <% loop centerLocations %>
                   <h2>$Title</h2>
                   <% loop Centers %>
    <h3>$Title</h3>
                      <p>$Description</p>
                   <% end_loop %>
                <% end_loop %>
             <% end_if %>
       </div>

    I'm using the above code to loop through the Location dataobject and inside of it I'm looping through Center dataobject. The problem I'm having is I can't get the centers to show inside their location.. any help?

    thanks

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: data object loop inside another loop not working Link to this post

    Hi Sajok,
    It looks like your page has many centers, buy your Center object only has one Location relationship. You can't loop a has one relationship.

    Try

    <div id="accordion">
    <% if Centers %>
    <% loop Centers %>
    <h2>$Title</h2>
    <h3>$Location.Title</h3>
    <p>$Location.Description</p>
    <% end_loop %>
    <% end_if %>
    </div>

  • sajok
    Avatar
    Community Member
    82 Posts

    Re: data object loop inside another loop not working Link to this post

    Hi Bambii7,

    I want the accordion options to be location names (ex: cities) not centers title. I want to loop through locations because each location has many centers that belong to it.

    Can you look at it again, and see why centers don't show in this case?

    Thanks

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: data object loop inside another loop not working Link to this post

    Hi sajok

    You can't loop your 'has many' as you haven't set it up - you need to add $has_many = array('Centers'=>'Center'); to your Location dataobject. That way, you won't have to manually set $Location->Centers in your centerLocations() method.

    Hope this helps

  • sajok
    Avatar
    Community Member
    82 Posts

    Re: data object loop inside another loop not working Link to this post

    Thanks Kinglozzer,

    I added the missing has_many to Location dataobject, and it works now.

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