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.

Archive

Our old forums are still available as a read-only archive.

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

Has Many Images


Reply

7 Posts   2540 Views

Avatar
UncleCheese

7 May 2008 at 3:12am 4085 Posts

What's the best way to deal with a has_many relationship to files or images? I've tried using a complex table field, but it's fairly awkward. You have to save the row before you upload an image, and this is particularly annoying if there is no other data than the image because the row can never get saved. (i.e. Title/Image allows you to save title first, then upload the image after, but what if it's just Image?)

What I want ultimately is to have a random image displayed on every page. I'm wondering what solutions might be out there. Should I be using the Files/Images tab for this? Or can it really be done at the page level?

Avatar
Willr

7 May 2008 at 10:22pm Forum Moderator, 5511 Posts

You could create a folder in the Files Section for the images then use a php function to select a file on random. There is also a HasMany upload form that I believe Andy has created for a project - http://open.silverstripe.com/changeset/46799 - Added a HasManyFile field.. Don't know what state that would be in to use.

Avatar
UncleCheese

5 June 2008 at 1:02am 4085 Posts

Looks like the HasMany file field has a ways to go. If I take your suggestion of using a PHP function to loop through a folder, how would I take advantage of the GD functions that I usually use with my Image objects?

Avatar
Willr

5 June 2008 at 9:32am Forum Moderator, 5511 Posts

if you return Image DataObjects then you will have all the functions available!. Eg if you are saving a folder id with a dropdown field then you can do something as simple as

$files = DataObject::get("File", "ParentID = $this->FolderID");

that would return all the files in that folder

Avatar
UncleCheese

5 June 2008 at 1:05pm 4085 Posts

Interesting. So Silverstripe will figure out based on the file extension or mime-type which of those files are images?

I guess my concern is that I want to do a custom cropped resize on these images, and I usually extend the image class to do that by creating a generateMyCrop() function. Where would that fit into this approach?

Avatar
UncleCheese

7 June 2008 at 1:27am 4085 Posts

Any idea on this, willr, or am I out of luck?

Avatar
UncleCheese

10 June 2008 at 2:53am 4085 Posts

I solved this problem using a magic method called "newClassInstance()" that I found in the GalleryPage code. Love it!

$file = DataObject::get_one("File", "ParentID = $myFolder", true, "RAND()");
$image = $file->newClassInstance("myImageClass");