Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Create Array from File Object

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

Page: 1
Go to End
Author Topic: 1921 Views
  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Create Array from File Object Link to this post

    I'm trying to create a function to download all product images from a site as a zip file. I need to get the all the images paths as an array to pass to a zip file creator function. I'm halfway there. I can get get all the images needed as a DataObject but now I need to pass just the Filename column as an array. This is what I have so far:

    public function DownloadAllImages(){
          //$records = DataObject::get("ToyImage", "Type= 'Normal'");
          $records = DB::query("SELECT * FROM ToyImage WHERE Type ='Normal'");
          $returnImages = new DataObjectSet();
          $filePaths = array();
          
          
          if ($records) {
             //return $records;
             foreach($records as $record){
                
                $imageID = $record['AttachmentID'];
                $returnImages->push(new ArrayData(array(
                                            'Name' => DataObject::get_by_id('File', $imageID)
                                            )));
             }
             
             return $returnImages;
          } else {
             return false;
          }
       }

    Thanks in advance

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: Create Array from File Object Link to this post

    Hi there,

    Try map():

    return $returnImages->map('ID','Filename');

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: Create Array from File Object Link to this post

    That returns an empty array.

    If I change it to $returnImages->map('ID','Name'); I get only last DataObject;

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: Create Array from File Object Link to this post

    Ok, then let's keep it simple...

    public function DownloadAllImages(){
       $records = DataObject::get("ToyImage", "Type= 'Normal'");
       $filePaths = array();

       if ($records) {
          foreach($records as $record){
             $File = DataObject::get_by_id('File', $record->AttachmentID);
             $filePaths[] = $File->Filename;
          }
          return $filePaths;
       } else {
          return false;
       }
    }

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: Create Array from File Object Link to this post

    Thanks That Got it.

    The simpler the better I guess

  • SSadmin
    Avatar
    Community Member
    90 Posts

    Re: Create Array from File Object Link to this post

    Hey, Thanks for the post.
    It really helps lots.
    I was trying to use ing the strucutre try to get the image from the imageGallery module.
    But from the code shown above, we can only get the url for the image instead of the Image Class Object. so the image will be huge, and cannot be applied for re-size function.
    Is there a way we can construct a Image Class with the url we got and re-apply re-size function?!?!

    Regards
    Alex

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: Create Array from File Object Link to this post

    Yes this function only returns the path as thats what I needed the images paths in an array. You'd need to apply the resize image class first then generate then pull the URL of the resized image. But if you only need it in a template then just run the Image.SetWidth() control and $Attachment.URL will get you the path

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