Jump to:

3432 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Display data on page from many_many relationships in DataObjects

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1164 Views
  • mspacemedia
    Avatar
    Community Member
    7 Posts

    Display data on page from many_many relationships in DataObjects Link to this post

    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.

  • mspacemedia
    Avatar
    Community Member
    7 Posts

    Re: Display data on page from many_many relationships in DataObjects Link to this post

    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 !

    1164 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.