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 /

[SOLVED] Help Creating Top 10 Products List


Reply


6 Posts   1838 Views

Avatar
zenmonkey

Community Member, 532 Posts

26 September 2009 at 4:30am

Edited: 04/10/2009 3:07am

I'm trying to create a page that will hold a Top 10 Products list

Here is my Top10Page controller

class Top10Page extends Page {
   public static $db = array(
                      'LifestyleText' => 'HTMLVarchar(255)'   
   );
   
   public static $has_one = array(
                        
   );
   
   static $has_many = array (
                      'ExtraPageImages' => 'ExtraPageImages'/*,
                      'Products'=>'Product'*/
   );
   
   static $many_many = array ('Products'=>'Product'
                      );
   
   function getCMSFields() {
$fields = parent::getCMSFields();
   
    $source = DataObject::get('Product');
    $fields->addFieldToTab("Root.Content.Top10", new CheckboxSetField("Products","Top Ten Pages",$source));
    return $fields;
   }   
}

class Top10Page_Controller extends Page_Controller {
}

This will let me pick 10 product pages but doesn't allow for Countdown Ordering? Any Ideas?

Avatar
socks

Community Member, 190 Posts

26 September 2009 at 9:24am

Edited: 04/10/2009 9:53am

So it sounds like you need sorting on your list. I did something similar (with help from Motoservo) where I basically had an ArticlePage and I did a drop-down menu (instead of a checkbox) in the Admin to allow the client to add the Article to either a Primary or Secondary section on the Home Page. I also added a function to sort the list for the Secondary section because I had a more than one article. Here's what I did for your situation... it seems to work.

ProductPage.php

class ProductPage extends Page {
static $db = array(
    'ProductRating' => "Enum(array('Not Rated', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ), 'Not Rated')"
);

   function getCMSFields() {
      $fields = parent::getCMSFields();

      $options = array('NotRated' => 'Not Rated', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8','9' =>'9','10'=>'10');
      $field = new DropdownField('ProductRating', 'Rate the Product', $options);

$field = new DropdownField('ProductRating', 'Rate the Product', singleton('ProductPage')->dbObject('ProductRating')->enumValues());
      $fields->addFieldToTab('Root.Behaviour', $field, 'ShowInMenus'); // the showinmenus is for placement in the CMS, if left blank, drop-down menu will appear at bottom
      
      return $fields;
    }

}

Top10Holder.php

class Top10Holder_Controller extends Page_Controller {
   
function ShowTopTen() {
    return DataObject::get("ProductPage", "", "ProductRating ASC", "", "10");
}

}

Top10Holder.ss

<ol>
<% control ShowTopTen %>
   <li>
      <h1>$Title</h1>
      $Content
   </li>
<% end_control %>
</ol>

Avatar
Pigeon

Community Member, 243 Posts

26 September 2009 at 10:49am

Just a quick tip:

When you use an Enum field in your $db array, then there is no need to redefine the Enum array for a drop down. Its bad to do as it is redundant and can lead to inconsistancies. Instead, use the following code to mean that the array is only ever edited in the $db.

$field = new DropdownField('ProductRating', 'Rate the Product', singleton('ProductPage')->dbObject('ProductRating')->enumValues());

See original code: http://doc.silverstripe.org/doku.php?id=dropdownfield#populate_with_enum-values

This simply means that you have one place to go to edit the dropdown! Much better for maintenance

Avatar
socks

Community Member, 190 Posts

26 September 2009 at 12:37pm

Thanks Pigeon for the tip, I thought that looked a little ugly.

Avatar
zenmonkey

Community Member, 532 Posts

26 September 2009 at 12:55pm

Edited: 26/09/2009 12:56pm

Rating the product won't work as this would be a feature that changes quite often and its possible there would be more than one Top 10 page, hence the many_many relationship. I need to be able to add and sort products on multiple top 10 pages.

I though about creating multiple drop downs on the Top10Page object each with a list of products. But I was having trouble wrapping my head around the relationships. And just storing the page ID for the products and using it in individual functions for each ranking kept causing me errors.

I really think my problem is the relationship between the the Top10Page and ProductPage, because as it sits now I can select 10 items and then just call the data in the template as if its the same DataObject.

Maybe a Sorted DataObjectManager is the best way to go.

Avatar
zenmonkey

Community Member, 532 Posts

4 October 2009 at 3:07am

I ended up just making 10 different selectors that save the PageID, then just used that value to ::get_by_id. I guess good ol' KISS was was best