I hope someone can shed some light on this for me. I have two DataObjects (Products/ProductTypes) with a many_many and belongs_many_many relationship defined between them. In my modelAdmin I can assign my Products to their respective ProductTypes. What I want to do now is display the Products based on a ProductType link (formed as /browse/by/productType - where productType is its stored URLSegment) that has been clicked on.
Products:
class Product extends DataObject
{
static $db = array(
'Title' => 'Varchar(255)',
'Description' => 'HTMLText',
'TechnicalData' => 'HTMLText',
'Price' => 'Decimal(6,2)',
'URLSegment' => 'Varchar(255)',
'MetaTitle' => 'Varchar(255)'
);
//Set our defaults
static $defaults = array(
'Title' => 'New Product',
'URLSegment' => 'new-product'
);
static $has_one = array(
'Image' => 'Image',
'ProductImage' => 'Image'
);
//Relate to the category pages
static $belongs_many_many = array(
'Categories' => 'CategoryPage',
'ProductTypes' => 'ProductType'
);
//Fields to show in ModelAdmin table
static $summary_fields = array(
'Title' => 'Title',
'URLSegment' => 'URLSegment',
'Price' => 'Price (£)'
);
//Add an SQL index for the URLSegment
static $indexes = array(
"URLSegment" => true
);
//Fields to search in ModelAdmin
static $searchable_fields = array (
'Title',
'URLSegment',
'Description',
'Categories.ID' => array(
'title' => 'Category'
),
'ProductTypes.ID' => array(
'title' => 'ProductTypes'
) );
ProductType:
static $db = array(
'Title' => 'Varchar(255)',
'Description' => 'HTMLText',
'URLSegment' => 'Varchar(255)'
);
// Set Default Entries
static $defaults = array(
'Title' => 'New Product Type',
'URLSegment' => 'new-product-type'
);
static $has_one = array(
'ProductBanner' => 'Image'
);
static $many_many = array(
'Products' => 'Product'
);
// Fields in ModelAdmin
static $summary_fields = array(
'Title' => 'Title',
'URLSegment' => 'URLSegment',
);
// Add SQL Index for URLSegment
static $indexes = array(
"URLSegment" => true
);
And a browsePage to control it the links are generated in productTypes by an overriding link function:
class BrowsePage_Controller extends Page_Controller
{
static $allowed_actions = array(
'by'
);
//Return the list of products for this category
public function getProductType()
{
$Params = $this->getURLParams();
$URLSegment = Convert::raw2sql($Params['ID']);
if($URLSegment )
{
return DataObject::get('ProductType', "URLSegment = '" . $URLSegment . "'");
}
}
//Shows the Product detail page
function by()
{
//Get the Products
if($Products = $this->getProductType())
{
$Data = array( 'Products' => $Products );
return $this->customise($Data)->renderWith(array('BrowsePage', 'Page'));
}
else
{
return $this->httpError(404, 'Sorry that product could not be found');
}
}
The above when <% control Products %> is called just returns the ProductType details not the associated products ?
Any ideas where this is going wrong ?
Thanks in advance.