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.

Data Model Questions

Display data on page from many_many relationships in DataObjects


Reply

2 Posts   1189 Views

Avatar
mspacemedia

20 January 2011 at 1:11pm Community Member, 7 Posts

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.

Avatar
mspacemedia

20 January 2011 at 11:57pm Community Member, 7 Posts

Okay,

This was fixed by ajshort on irc for me and oh how simple it is...

Two lines needed changing:

return DataObject::get('ProductType', "URLSegment = '" . $URLSegment . "'");

to

return DataObject::get_one('ProductType', "URLSegment = '" . $URLSegment . "'");

Nice and simple, then in the browsePage.ss:

<% control Products.Products %>

to return the related products across the dataobjects !!

Thank you AJShort for the answers !