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.

General Questions

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

data object loop inside another loop not working


Reply

5 Posts   654 Views

Avatar
sajok

3 June 2013 at 2:49pm (Last edited: 3 June 2013 2:50pm), Community Member, 82 Posts

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

Avatar
Bambii7

3 June 2013 at 7:01pm Community Member, 254 Posts

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>

Avatar
sajok

3 June 2013 at 10:06pm Community Member, 82 Posts

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

Avatar
kinglozzer

4 June 2013 at 3:18am (Last edited: 4 June 2013 3:21am), Community Member, 146 Posts

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

Avatar
sajok

4 June 2013 at 6:48am Community Member, 82 Posts

Thanks Kinglozzer,

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