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.

General Questions

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

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

nested controls: get values from parent


Reply

4 Posts   1889 Views

Avatar
dacar

6 October 2009 at 10:34am (Last edited: 6 October 2009 10:35am), Community Member, 168 Posts

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 ([url]http://doc.silverstripe.org/doku.php?id=datamodel#joining[/url])

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

6 October 2009 at 3:33pm Community Member, 283 Posts

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

6 October 2009 at 4:51pm Forum Moderator, 5511 Posts

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

Avatar
dacar

6 October 2009 at 10:00pm Community Member, 168 Posts

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;
   }