Ok. I wrote code like you suggest me.
TrailerCategory
class TrailerCategory extends Page {
static $has_many = array('Trailers' => 'Trailer');
static $singular_name = 'TrailerCategory';
static $plural_name = 'TrailerCategories';
function getCMSFields() {
$fields = parent::getCMSFields();
$trailersTable = new DataObjectManager($this, 'Trailers','Trailer',
array('getThumbnail' => _t('ProductCategory.PREVIEW'),'Title' => _t('ProductCategory.TITLE')),'getCMSFields_forPopup');
$trailersTable->setParentClass('TrailerCategory');
$fields->addFieldToTab('Root.Content.Trailers', $trailersTable);
$wheelType = new DataObjectManager($this,_t('WheelType.LABEL'),'WheelType');
$fields->addFieldToTab('Root.Content.WheelType', $wheelType);
return $fields;
}
}
class TrailerCategory_Controller extends Page_Controller {}
Product
class Product extends DataObject {
static $singular_name = 'Product';
static $plural_name = 'Products';
static $db = array(
'Title' => 'Text',
'Price' => 'Currency',
'Description' => 'HTMLText',
'TopProduct' => 'Boolean'
);
static $has_one = array(
'Image' => 'Image'
);
static $has_many = array(
'ProductImages' => 'ProductImage'
);
public function getCMSFields_forPopup()
{
$fields = new FieldSet(
new TextField('Title', 'Product Title'),
new CurrencyField('Price', 'Price'),
new SimpleWysiwygField ('Description', 'Product Description'),
new CheckboxField('TopProduct', 'Top product')
);
$imageDataObject = new ImageDataObjectManager($this, 'ProductImages','ProductImage', 'Attachment');
$imageDataObject->setAllowedFileTypes(array('jpg','gif','png'));
$imageDataObject->setBrowseButtonText('Upload (JPG,GIF,PNG)');
$imageDataObject->setPluralTitle('Images');
$fields->push($imageDataObject);
return $fields;
}
function getThumbnail(){
if($images = DataObject::get_one("ProductImage", "ProductId = {$this->ID}"))
return ($img = $images->Attachment()) ? $images->Thumbnail() : "no image";
return "no image";
}
}
class Product_Controller extends Page_Controller {
}
Trailer
class Trailer extends Product {
static $singular_name = 'Trailer';
static $plural_name = 'Trailers';
static $has_one = array(
'TrailerStuff' => 'TrailerStuff',
'TrailerCategory' => 'TrailerCategory'
);
public function getCMSFields_forPopup() {
$fields = parent::getCMSFields_forPopup();
$trailerStuff = new DataObjectManager($this,'TrailerStuff','TrailerStuff');
$fields->push($trailerStuff);
return $fields;
}
}
class Trailer_Controller extends Product_Controller {
}
... and stuff
TrailerStuff
class TrailerStuff extends DataObject {
static $db = array(
'GrossWeight' => 'Int',
'TrailerWeight' => 'Int',
'LoadWeight' => 'Int',
'Length' => 'Int',
'Axels' => 'Int',
'Brake' => 'Boolean',
'TopProduct' => 'Boolean',
);
static $has_one = array(
'Trailer' => 'Trailer',
'WheelType' => 'WheelType'
);
function getCMSFields() {
$wheelType = Dataobject::get("WheelType");
if ($wheelType) {
$wheelType = $wheelType->toDropdownMap('ID', 'Type');
}
$fields = new FieldSet(
new NumericField('GrossWeight', 'Gross Weight'),
new NumericField('TrailerWeigh', 'Trailer Weight'),
new NumericField('LoadWeight', 'Load Weight'),
new NumericField('Width', 'Width'),
new NumericField('Length', 'Length'),
new NumericField('Axels', 'Axels'),
new CheckboxField('Brake', 'Brake'),
new DropdownField(
'WheelTypeID',
'Please choose a wheel type',
$wheelType)
);
return $fields;
}
}
for stuff WheelType
WheelType
class WheelType extends DataObject {
static $db = array(
'Type' => 'Text',
);
static $has_one = array(
'TrailerStuff' => 'TrailerStuff'
);
function getCMSFields() {
$fields = new FieldSet(
new TextField('Type', 'Wheel type')
);
return $fields;
}
}
I catch error Couldn't run query: SELECT "Product"."ClassName", "Product"."Created", "Product"."LastEdited", "Product"."Title", "Product"."Price", "Product"."Description", "Product"."TopProduct", "Product"."ImageID", "Trailer"."TrailerStuffID", "Trailer"."TrailerCategoryID", "Product"."ID", CASE WHEN "Product"."ClassName" IS NOT NULL THEN "Product"."ClassName" ELSE 'Product' END AS "RecordClassName" FROM "Product" LEFT JOIN "Trailer" ON "Trailer"."ID" = "Product"."ID" WHERE ("Product"."ClassName" IN ('Trailer')) AND ("TrailerCategoryID" = '35') ORDER BY Created DESC LIMIT 10 OFFSET 0 Column 'TrailerCategoryID' in where clause is ambiguous
What i get wrong?