Jump to:

3433 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [SOLVED] Help Creating Top 10 Products List

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

Page: 1
Go to End
Author Topic: 1749 Views
  • zenmonkey
    Avatar
    Community Member
    527 Posts

    [SOLVED] Help Creating Top 10 Products List Link to this post

    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?

  • socks
    Avatar
    Community Member
    190 Posts

    Re: [SOLVED] Help Creating Top 10 Products List Link to this post

    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>

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: [SOLVED] Help Creating Top 10 Products List Link to this post

    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

  • socks
    Avatar
    Community Member
    190 Posts

    Re: [SOLVED] Help Creating Top 10 Products List Link to this post

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

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: [SOLVED] Help Creating Top 10 Products List Link to this post

    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.

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: [SOLVED] Help Creating Top 10 Products List Link to this post

    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

    1749 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.