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.

Template Questions /

Yet another nested controls problem


Reply


7 Posts   2872 Views

Avatar
Bezzen

Community Member, 6 Posts

17 August 2010 at 2:28am

I'm trying to build a site in Silverstripe and so far it has proven to be quite frustrating. Let's just say that if I wrote it from the ground up in plain PHP it would be live now. ;)

I'm having some nested controls that I've sort of got working after lots of headaches but I just can't close the bag on this one. It's probably dead simple, but I haven't managed to find anythjing that can help me. Google certainly isn't my friend in this case.

I have to separate MySQL queries. The first one shows a list of companies and the second one counts the number of products by that company. This is supposed to be output like this:

Company 1 - (2 products)
Company 2 - (2 product)
etc.

The following dummy I set up works like I want it to:

<% control TheCompanies %>
<a href="/companies/$ID/">$CompanyName</a> -

<% control Top.TheCompany(10) %>
($TheNumber products)<br />
<% end_control %>
<% end_control %>

This dummy now shows the name for company and then shows the number of products for company with ID number 10.

Now I of course want the ID number to be relative to the right company ID (not just number 10). So I've tried the following:

<% control Top.TheCompany($ID) %>

and

<% control Top.TheCompany($TheCompanies.ID) %>

But that doesn't work at all. How do I get the $ID from TheCompanies control block into the TheCompany control block?

Avatar
swaiba

Forum Moderator, 1808 Posts

17 August 2010 at 6:38am

Hi,

You might have got it live by now... but you wouldn't have got all the stuff ss has in it if you worte it yourself ;-)

Anyway...

there is only one pass made of the template so it cannot be passed the $ID into a function and you'll instead have to preload all the data into the TheCompanies, I'd do the following...

function TheCompanies ()
{
   $dosCompanies = DataObject::get('Company');//or however you do that...
   if ($dosCompanies)
   {
      foreach ($dosCompanies as $doCompany)
      {
         $dosSubSet = DataObject::get('CompanyData','ID = '.$doCompany->ID);
         $doCompany->TheCompanySubDataObject = $dosSubSet;
         $doCompany->TheCount = $dosSubSet->TotalItems();
      }
   }
   return $dosCompanies;
}

<% control TheCompanies %>
   $CompanyName
   $TheCount
   <% control TheCompanySubDataObject %>
      $CompanySubDataField
   <% end_control %>
<% end_control %>

Untested but it is in the right direction...

Barry

Avatar
Bezzen

Community Member, 6 Posts

17 August 2010 at 8:23am

Edited: 17/08/2010 8:25am

Thanks for the reply, swaiba. I'll try and see if I can get your method working tonight.

Silverstripe's way of doing things is all Greek to me now, but I'm sure the penny will drop soon. :D

Avatar
Bezzen

Community Member, 6 Posts

17 August 2010 at 9:17am

Worked like a charm straight out of the box!

Thanks a lot Barry! :)

Avatar
sashion

Community Member, 23 Posts

2 June 2011 at 6:52pm

That was EXACTLY what I needed! Thanx for both - the already asked question and the answer ;)

cheers

Avatar
swaiba

Forum Moderator, 1808 Posts

2 June 2011 at 9:02pm

You are both very welcome :)

Avatar
ajshort

Community Member, 244 Posts

2 June 2011 at 9:45pm

Edited: 02/06/2011 9:48pm

This is a bad way to do this - it generates a huge amount of DB queries. You'd probably want to query the database in one hit for company name and the count of matching companydata objects, then push this into a DataObjectSet and render it.