Jump to:

3395 Posts in 977 Topics by 853 members

Template Questions

SilverStripe Forums » Template Questions » Yet another nested controls problem

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

Page: 1
Go to End
Author Topic: 2473 Views
  • Bezzen
    Avatar
    Community Member
    6 Posts

    Yet another nested controls problem Link to this post

    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?

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Yet another nested controls problem Link to this post

    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

  • Bezzen
    Avatar
    Community Member
    6 Posts

    Re: Yet another nested controls problem Link to this post

    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.

  • Bezzen
    Avatar
    Community Member
    6 Posts

    Re: Yet another nested controls problem Link to this post

    Worked like a charm straight out of the box!

    Thanks a lot Barry!

  • sashion
    Avatar
    Community Member
    21 Posts

    Re: Yet another nested controls problem Link to this post

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

    cheers

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Yet another nested controls problem Link to this post

    You are both very welcome

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: Yet another nested controls problem Link to this post

    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.

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