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.

Data Model Questions /

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

Create Array from File Object


Go to End


7 Posts   2994 Views

Avatar
zenmonkey

Community Member, 545 Posts

14 July 2010 at 6:17am

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

Avatar
Devlin

Community Member, 344 Posts

15 July 2010 at 1:18am

Hi there,

Try map():

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

Avatar
zenmonkey

Community Member, 545 Posts

15 July 2010 at 2:11am

That returns an empty array.

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

Avatar
Devlin

Community Member, 344 Posts

15 July 2010 at 3:04am

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;
	}
}

Avatar
zenmonkey

Community Member, 545 Posts

15 July 2010 at 4:22am

Thanks That Got it.

The simpler the better I guess

Avatar
SSadmin

Community Member, 90 Posts

6 August 2010 at 12:13pm

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

Avatar
zenmonkey

Community Member, 545 Posts

7 August 2010 at 12:43am

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