I need to associate multiple images with a page. Do I use has_many Image on the page? Or do I use has_many DataObject on the page where the DataObject has_one Image? What's the best way to do this?
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.
I think it depends on how many images you need and how you plan on displaying them.
If all you want is a Gallery style setup, then setting up a new DataObject for the page would be your best bet (of go check out the Gallery Module...). The SS Book has a good demo you can adjust easily using Jobs and Job Categories... I'm sure there's a similar tutorial around here somewhere.
If you have set places you want them all to go, you might want to consider just setting up individual fields for each item on the Page and coding their placeholders in to the layout.
Like marblegravy said, it depends how you want to display them. If you literally just want to relate many images, then $has_many is fine and i would use something like: http://doc.silverstripe.org/doku.php?id=modules:DataObjectManager#imagedataobjectmanager to manage them :)
I want to display a gallery of images per product & a main image. I have;
class Product extends DataObject
{
static $db = array (
'Name' => 'Text'
);
static $has_one = array (
'image1' => 'Image',
);
static $has_many = array (
'gallery' => 'Image'
);
public function getCMSFields_forPopup()
{
$fields = new FieldSet(
new TextField('Name'),
new ImageField("image1","Main image",null,null,null,"assets/myFolder/"),
);
$fields->push(new ImageField("gallery","Image gallery",null,null,null,"assets/myFolder/"));
return $fields;
}
}
The upload/attach for the main image is fine, but in the popup 'Image gallery' appears as a single File upload.
Any ideas?