Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Has Many Images

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

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

Page: 1
Go to End
Author Topic: 2512 Views
  • UncleCheese
    Avatar
    4085 Posts

    Has Many Images Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Has Many Images Link to this post

    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.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Has Many Images Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Has Many Images Link to this post

    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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Has Many Images Link to this post

    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?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Has Many Images Link to this post

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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Has Many Images Link to this post

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

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