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

Bidimensional array


Go to End


3 Posts   2404 Views

Avatar
Devis

Community Member, 4 Posts

15 July 2010 at 5:30am

Edited: 15/07/2010 7:07am

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

Avatar
Willr

Forum Moderator, 5523 Posts

15 July 2010 at 8:48am

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

Avatar
Devis

Community Member, 4 Posts

15 July 2010 at 10:31pm

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 %>