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   2551 Views

Avatar
UncleCheese

4086 Posts

7 May 2008 at 3:12am

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

Forum Moderator, 5513 Posts

7 May 2008 at 10:22pm

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

4086 Posts

5 June 2008 at 1:02am

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

Forum Moderator, 5513 Posts

5 June 2008 at 9:32am

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

4086 Posts

5 June 2008 at 1:05pm

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

4086 Posts

7 June 2008 at 1:27am

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

Avatar
UncleCheese

4086 Posts

10 June 2008 at 2:53am

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");