Jump to:

23368 Posts in 18143 Topics by 2863 members

General Questions

SilverStripe Forums » General Questions » [SOLVED] Relating many image files to a DO

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 698 Views
  • theade
    Avatar
    Community Member
    9 Posts

    [SOLVED] Relating many image files to a DO Link to this post

    Hi, I'm a total newb.

    Bit stumped on my first project.

    I have a DO class, Bike. It needs to have X images, so it has a has_many relationship to a class, BikeImage, that just extends File, and declares the has_one relationship back to bike.

    All good. The DB looks right - I have a join table between File and Bike.

    I installed dataobjectmanager and uploadify for the back-end to be nice. And it mostly does, very nice in fact, apart from the crucial relationship between Bike and Image - the join table doesn't get populated when I update an existing Bike and give it an image (using 'import' which I'm assuming is correct).

    I've implemented the form both using modeladmin and through a class extending page with getCMSFields, but I get the same thing. Either if I add an image first through Files & Images, or if I upload it from the CRUD form and import it.

    The requests looks good (editing the first bike in the db):

    http://mydoman.local/admin/bikeadmin/Bike/1/EditForm?action_doSave=Save

    with form data

       .   Title:Some bike
       .   Description:this is a great bike
       .   NumberOfGears:27
       .   WheelSize:26
       .   FromPrice:800
       .   AttachedFiles:
       .   UploadFolderID_Form_EditForm_AttachedFiles:1
       .   NewFolder_Form_EditForm_AttachedFiles:
       .   ImportFolderID_Form_EditForm_AttachedFiles:
       .   ImportFiles[]:46
       .   AttachedFiles[]:46
       .   ID:1

    46 is the image that is in the file table that I selected. No errors. Just don't get why it won't update the DB.

    Some code…

    My page class:

    class BikesPage extends Page {
       public function getCMSFields()
       {
          $f = parent::getCMSFields();
          $f->addFieldToTab("Root.Content.Bikes", new DataObjectManager(
             $this,
             'Bikes',
             'Bike',
             array(//some props),
             'getCMSFields_forPopup'
          ));
          $f->push( new MultipleFileUploadField('AttachedFiles','Upload several files') );    
          return $f;
       }
    }
    ...   


    My DO:

    class Bike extends DataObject {
       static $db = array(
       //some props
       );

       static $has_many = array(
          'BikeImages' => 'BikeImage'
       );
       
       public function getCMSFields() {
       
          $fields = new FieldSet();
          //push some props
          $fields->push( new MultipleFileUploadField('AttachedFiles','Upload several files') );       
          return $fields;       
       }
    }


    BikeImage

    class BikeImage extends File {
       static $has_one = array (
          'Image' => 'Bike'
       );
    }


    I'm stumped. It looks like it should work. But I guess I'm missing something key.

    Thanks very much.

  • Howard
    Avatar
    Community Member
    215 Posts

    Re: [SOLVED] Relating many image files to a DO Link to this post

    Hey, welcome!

    You need to link the bikes to the BikesPage. In your BikesPage class include:

    static $has_many = array(
    'Bikes' => 'Bike'
    );

  • theade
    Avatar
    Community Member
    9 Posts

    Re: [SOLVED] Relating many image files to a DO Link to this post

    Hey Howard,

    Thanks for getting back to me.

    So I tried your suggestion - and did the reverse relationship in bike back to page - but I get the same thing. No image relationship.

    Any other ideas? I'm so close to getting this nailed... Well, it feels that way...

    Thanks

  • theade
    Avatar
    Community Member
    9 Posts

    Re: [SOLVED] Relating many image files to a DO Link to this post

    Actually, Howard, I take that back - your suggestion has really helped. I've now fixed it - there was another problem that stopped it working.

    Thanks again.

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