Ok, i've implemented that ImageAttachment class directly to the Product page and got it workin how i want... however; i have dropped the has_one Image that Product has for each one, and now i need the first of my multiple images to be shown on the front of the product category (e.g. in featured products and nested product groups). I'll post my code below, hopefully someone can help me fathom my way through into getting to the object through all the has ones and has many's through the ecommerce module.
<?php
class ImageAttachment extends DataObject {
static $db = array(
'Name' => 'Text'
);
static $has_one = array(
'Image' => 'Resize_Image',
'Product' => 'Product'
);
static $field_names = array('Name' => 'Name');
function getCMSFields_forPopup() {
$fields = new FieldSet();
$fields->push(new TextField('Name', 'Name'));
$fields->push(new ImageField('Image', 'Image'));
return $fields;
}
}
class Resize_Image extends Image {
static $db = null;
function generateThumbnail($gd) {
$gd->setQuality(80);
return $gd->paddedResize(140,100);
}
function generateContentImage($gd) {
$gd->setQuality(90);
return $gd->resizeByWidth(200);
}
function generateLargeImage($gd) {
$gd->setQuality(90);
return $gd->resizeByWidth(600);
}
}
?>
My Modified Product class
/**
* Image Support
*/
static $has_one = array(
'ImageThumbnail' => 'Product_Image',
'Image' => 'Image'
);
static $has_many = array(
'ImageAttachments' => 'ImageAttachment'
);
static $defaults = array(
'AllowPurchase' => true
);
/**
* Create the fields for a product within the CMS
*/
function getCMSFields() {
$fields = parent::getCMSFields();
// standard extra fields like weight and price
$fields->addFieldToTab("Root.Content.Main", new TextField("Weight", "Weight (kg)", "", 12));
$fields->addFieldToTab("Root.Content.Main", new TextField("Price", "Price", "", 12));
$fields->addFieldToTab("Root.Content.Main", new TextField("Model", "Author", "", 50));
$fields->addFieldToTab("Root.Content.Main", new TextField("InternalItemID","Product Code","",7));
$imagetable = new ComplexTableField(
$this,
'ImageAttachments', // relation name
'ImageAttachment', // object class
ImageAttachment::$field_names, // fields to show in table
ImageAttachment::getCMSFields_forPopup(), // form that pops up for edit
"ProductID = {$this->ID}", // a filter to only display item associated with this page
"Name ASC" // Sort by name
);
$fields->addFieldToTab('Root.Content.Additional Images', $imagetable);
...
function ImageAttachment() {
if($obj = DataObject::get('ImageAttachment', "ProductID = $this->ID")) {
return $obj;
} else {
return false;
}
}