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.

General Questions /

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

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

data object loop inside another loop not working


Go to End


5 Posts   3331 Views

Avatar
sajok

Community Member, 82 Posts

3 June 2013 at 2:49pm

Edited: 03/06/2013 2:50pm

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

Community Member, 254 Posts

3 June 2013 at 7:01pm

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

Community Member, 82 Posts

3 June 2013 at 10:06pm

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

Community Member, 187 Posts

4 June 2013 at 3:18am

Edited: 04/06/2013 3:21am

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

Community Member, 82 Posts

4 June 2013 at 6:48am

Thanks Kinglozzer,

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