17488 Posts in 4473 Topics by 1978 members
|Go to End|
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?
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.
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?
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
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?
7 June 2008 at 1:27am
Any idea on this, willr, or am I out of luck?
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");
|Go to Top|