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.

Upgrading SilverStripe /

Ask questions about upgrading SilverStripe to the latest version.

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

SilverStripe 3.0.0-beta2 DataObjectSet equivelant for array in template


Reply


5 Posts   2907 Views

Avatar
MarijnKampf

Community Member, 164 Posts

31 May 2012 at 10:36pm

In SS 2.4 I often use array to store data that is accessible in both PHP and JavaScript. I use DataObjectSet to transform the array data to iterate over in a template.

2.4:
<?php
class Page extends SiteTree {
   function TestIt() {
      $a = array(
'one' => array('ID' => 'somedata', 'Name' => 'Name some data'),
'two' => array('ID' => 'moredata', 'Name' => 'Name some data'),
      );
      $al = new DataObjectSet($a);
//      Debug::Show($a);
//      Debug::Show($al);
      return $al;
   }
}

Template:
   <% control TestIt %><div>$ID $Name</div><% end_control %>

However, in 3.0 DataObjectSet is no longer available. The message tells me to use DataList or ArrayList instead.

DataList is out as it gives an error it needs table definition
[User Error] DataObject::buildSQL: Can't find data classes (classes linked to tables) for Array. Please ensure you run dev/build after creating a new DataObject.

And if I try using an ArrayList it doesn't show $ID or $Name. Should I be using a different approach or report a bug?

Cheers,

Marijn.

Avatar
simon_w

Forum Moderator, 474 Posts

1 June 2012 at 2:04pm

You need to wrap arrays in an ArrayData object for the template to be able to understand them.

Something like:

$al = new ArrayList(array_map(function($i) { return new ArrayData($i); }, $a);

Then <% loop TestIt %> will let you use $ID and $Name.

Avatar
MarijnKampf

Community Member, 164 Posts

1 June 2012 at 7:18pm

Thanks, never used the function construct that way in PHP.

Avatar
Wezzlee

Community Member, 28 Posts

14 July 2012 at 7:51pm

Hi,

I think I have the same issue.

I have used:

//FUNCTION TO FETCH IMAGES
   function getDirectoryList()
   {
    $url = $this->URLSegment;
    $folder="/home/website.nl/public_html/silverstripe/themes/blackcandy/images/photos/".$url;
   // create an object to hold directory list
    $icons = new DataObjectSet();
   
   // create a handler for the directory
   $handler = opendir($folder);
   
   // open directory and walk through the filenames
   while ($file = readdir($handler)) {
   
   // if file isn't this directory or its parent, add it to the results
   if ($file != "." && $file != ".." && substr($file,0,6)!="thumb_") {
       $icons->push(new ArrayData(array(
          'File' => $file
          )));
   }
   
   }
   
   // tidy up: close the handler
   closedir($handler);
   
   // done!
   //return $results;
    return $icons;
   
   }

Template:
<div style="display:none;">$URLSegment</div>                      
            <% control getDirectoryList %>
               <a href="/silverstripe/themes/blackcandy/images/photos/$Top.URLSegment/$File" rel="prettyPhoto[pp_gal]" $Title><img style="margin-right:4px; margin-bottom:4px;" src="/silverstripe/themes/blackcandy/images/photos/$Top.URLSegment/thumb_$File" border="0" width="52" height="52"></a>
            <% end_control %>

Because DataObjectSet is no longer available in SS 3.0. The message tells me to use DataList or ArrayList instead too. (User Deprecated] DataObjectSet->__construct is deprecated. Use DataList or ArrayList instead. Called from Page_Controller->getDirectoryList.)

I am not a PHP expert or programmer so I really do not have a clue how to make this SS 3.0 compliant.

Do you know how to make it SS 3.0 compatible?

Thank you,

Wesley
   

Avatar
nzblue_fish

Community Member, 13 Posts

26 July 2012 at 4:41pm

Hi Wezzlee,

Did you solve this problem?

I had written some code in SS 2.4 (as a very new SilverStriper) that used logic similar to yours and have just spent a few hours trying to figure out how to get it working in SS 3.0.

For what it's worth (and someone may post a better way of doing this) here is what I think you need to do based on what I've learnt:

Replace your $icons = new DataObjectSet(); with $icons = new ArrayList();

then

in your template use a <% loop getDirectoryList %> ... <% end_loop %> instead of using <% control ... %> .

Hope that helps.

Cheers, Innes (NZ)