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

nested controls: get values from parent


Go to End


4 Posts   3096 Views

Avatar
dacar

Community Member, 173 Posts

6 October 2009 at 10:34am

Edited: 06/10/2009 10:35am

Hi, i am trying to loop through one menu control [Menu(3)]. Within the loop i nested a custom control.

the result shoud look like this:

- item 1.$MenuTitle
item a. (item 1 $Top.Link)
item b. (item 1 $Top.Link)
item c. (item 1 $Top.Link)
- item 2.$MenuTitle
item a. (item 2 $Top.Link)
item b. (item 2 $Top.Link)
item c. (item 2 $Top.Link)
- item 3.$MenuTitle
item a. (item 3 $Top.Link)
item b. (item 3 $Top.Link)
item c. (item 3 $Top.Link)
- item 4.$MenuTitle
item a. (item 4 $Top.Link)
item b. (item 4 $Top.Link)
item c. (item 4 $Top.Link)

The Problem is, that you It will NOT return the additionally joined data (http://doc.silverstripe.org/doku.php?id=datamodel#joining)

How can i get the right Data?

P.S.There is another small thing: if you use s.t. like: $Top.Link/show/$ID you will have a double slash, because $Top.Link already contains one "/" (gesundheitswesen//show/6). How can i get rid of it?

Greetings, Carsten.

Template:

<div id="banner">
<div class="banner_top">&nbsp;</div>
<div class="banner_inner">
<ul class="menu">
<% control Menu(3) %>
<li class="sublevel1">$MenuTitle
<ul>
<% control getFirmenforBanner %>
<li class="sublevel2">
<a href="$Top.Link/show/$ID">$Firmenname $URLSegment</a>
</li>
<% end_control %>
</li>
</ul>
</li>
<% end_control %>
</ul>
</div>
<div class="banner_end">&nbsp;</div>
</div>

Page.php (model):

function getFirmenforBanner() {
// Tabelle + Where + Sort + JOIN + LIMIT
$firmen = DataObject::get("Stammdaten", "", "Stammdaten.Firmenname", "", "");
//Debug::show($firmen);
//die;
return $firmen;

}

Avatar
dalesaurus

Community Member, 283 Posts

6 October 2009 at 3:33pm

You were close, just had to make it to SQL Query per the instructions in the link you posted

http://doc.silverstripe.org/doku.php?id=sqlquery&s=sqlquery#transforming_a_result_to_dataobjectset

Avatar
Willr

Forum Moderator, 5523 Posts

6 October 2009 at 4:51pm

And for $Top.Link/show/$ID you would just remove the / before the show - {$Top.Link}show/$ID

Avatar
dacar

Community Member, 173 Posts

6 October 2009 at 10:00pm

Thanks Dalesaurus, thanks Willr,

i got it all working. If you read the docs carefully you can nearly solve every problem. To get all Data i had to build a foreach-loop.

Here is the code from my controller:

function getFirmenforBanner() {
$sqlQuery = new SQLQuery();
$sqlQuery->select = array(
'Firmenname AS Firmenname',
'Bereich AS Bereich',
'SiteTree.URLSegment AS URLSegment',
'SiteTree.Title AS Title',
'Stammdaten.ClassName AS ClassName',
'Stammdaten.ClassName AS RecordClassName',
'Stammdaten.ID AS ID'
);
$sqlQuery->from = array(
"Stammdaten",
"LEFT JOIN SiteTree ON Stammdaten.Bereich = SiteTree.ID"
);
$sqlQuery->where = array(
"SiteTree.ShowInMenus = 1 AND Bereich = ".$this->ID.""
);
$result = $sqlQuery->execute();
$firmen = new DataObjectSet();
foreach($result as $row) {
$firmen->push(new ArrayData($row));
}

return $firmen;
}