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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

[SOLVED] Relating many image files to a DO


Go to End


4 Posts   1980 Views

Avatar
theade

Community Member, 10 Posts

11 June 2012 at 7:52pm

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.

Avatar
Howard

Community Member, 215 Posts

11 June 2012 at 8:50pm

Hey, welcome!

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

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

Avatar
theade

Community Member, 10 Posts

12 June 2012 at 9:53am

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

Avatar
theade

Community Member, 10 Posts

12 June 2012 at 10:11am

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.