21309 Posts in 5738 Topics by 2603 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1154 Views |
-
Bidimensional array

15 July 2010 at 5:30am Last edited: 15 July 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
-
Re: Bidimensional array

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
-
Re: Bidimensional array

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 %>
| 1154 Views | ||
|
Page:
1
|
Go to Top |


