Jump to:

23489 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » nested controls: get values from parent

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

Page: 1
Go to End
Author Topic: 1877 Views
  • dacar
    Avatar
    Community Member
    168 Posts

    nested controls: get values from parent Link to this post

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

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: nested controls: get values from parent Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: nested controls: get values from parent Link to this post

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

  • dacar
    Avatar
    Community Member
    168 Posts

    Re: nested controls: get values from parent Link to this post

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

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