Jump to:

1156 Posts in 2568 Topics by 408 members

Upgrading SilverStripe

SilverStripe Forums » Upgrading SilverStripe » SilverStripe 3.0.0-beta2 DataObjectSet equivelant for array in template

Ask questions about upgrading SilverStripe to the latest version.

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

Page: 1
Go to End
Author Topic: 2552 Views
  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    SilverStripe 3.0.0-beta2 DataObjectSet equivelant for array in template Link to this post

    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.

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: SilverStripe 3.0.0-beta2 DataObjectSet equivelant for array in template Link to this post

    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.

  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    Re: SilverStripe 3.0.0-beta2 DataObjectSet equivelant for array in template Link to this post

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

  • Wezzlee
    Avatar
    Community Member
    28 Posts

    Re: SilverStripe 3.0.0-beta2 DataObjectSet equivelant for array in template Link to this post

    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
       

  • nzblue_fish
    Avatar
    Community Member
    13 Posts

    Re: SilverStripe 3.0.0-beta2 DataObjectSet equivelant for array in template Link to this post

    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)

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