Jump to:

23552 Posts in 19364 Topics by 2891 members

General Questions

SilverStripe Forums » General Questions » Bidimensional array

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: 1523 Views
  • Devis
    Avatar
    Community Member
    4 Posts

    Bidimensional array Link to this post

    Just wondering how to display a bi-dimensional DataObjectSet in a template:

    In the Page model I have this method:

    public function getGroups() {
    ....if (null === $this->groups) {
    ........$groups = DataObject::get('MyGroup', 'ParentID = ' . $this->ID, 'Sort', '');
    ........if ($groups) {
    ............foreach($groups as $group) {
    ................$items = DataObject::get('MyItem', "ParentID = {$group->ID}", 'Sort', '');
    ................if ($items) {
    ....................if (null === $this->groups) {
    ........................$this->groups = new DataObjectSet();
    ....................}
    ....................$this->groups->push($items);
    ................}
    ............}
    ........}
    ....}
    ....return $this->groups;
    }

    How do I cycle the data to show Groups and Items ?

    <% if Groups %>
    ....<% control Groups %>
    ........Group name: $Name
    ........<% control ???? %>
    ............Item name: ????
    ........<% end_control %>
    ....<% end_control %>
    <% end_if %>

    thank you

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Bidimensional array Link to this post

    To be able to iterate over a array in the template it must be a ArrayData object (or another dataobject set) for example

    $items = DataObject::get('MyItem', "ParentID = {$group->ID}");

    $items->Foo = new ArrayData(array('Title' => 'Hi'));

    Then in the template you can do

    <% control Groups %>
    <% control Foo %>
    $Title
    <% end_control %>
    <% end_control %>

    Will output 'Hi' for each group

    If you need to pass a set of data then you can make Foo a dataobjectset of arraydata object

    ..
    $myset = new DataObjectSet();
    $myset->push(new ArrayData(array('Title'=> 'One'));
    $myset->push(new ArrayData(array('Title'=> 'Two'));
    ..
    $item->Foo = $myset;

    Then the template from before will output 'One' 'Two' for each group

  • Devis
    Avatar
    Community Member
    4 Posts

    Re: Bidimensional array Link to this post

    Thanks Will for pointing me in the right direction, however I think there was a small error: Foo needs to be added to each Row and not to the Rowset.

    Here's the code.

    public function getGroups() {
    ....if (null === $this->groups) {
    ........$groups = DataObject::get('MyGroup', 'ParentID = ' . $this->ID, 'Sort', '');
    ........if ($groups) {
    ............foreach($groups as $group) {
    ................$group->Items = DataObject::get('MyItem', "ParentID = {$group->ID}", 'Sort', '');
    ................if ($group->Items) {
    ....................if (null === $this->groups) {
    ........................$this->groups = new DataObjectSet();
    ....................}
    ....................$this->groups->push($group);
    ................}
    ............}
    ........}
    ....}
    ....return $this->groups;
    }

    In the template:

    <% if Groups %>
    ....<% control Groups %>
    ........Group name: $Name
    ........<% control Items %>
    ............Item name: $Name
    ........<% end_control %>
    ....<% end_control %>
    <% end_if %>

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