3212 Posts in 847 Topics by 809 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1949 Views |
-
Yet another nested controls problem

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?
-
Re: Yet another nested controls problem

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
-
Re: Yet another nested controls problem

17 August 2010 at 8:23am Last edited: 17 August 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.
-
Re: Yet another nested controls problem

17 August 2010 at 9:17am
Worked like a charm straight out of the box!
Thanks a lot Barry!
-
Re: Yet another nested controls problem

2 June 2011 at 6:52pm
That was EXACTLY what I needed! Thanx for both - the already asked question and the answer ;)
cheers
-
Re: Yet another nested controls problem

2 June 2011 at 9:45pm Last edited: 2 June 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.
| 1949 Views | ||
|
Page:
1
|
Go to Top |



