Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » sorting by related fields

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1350 Views
  • tmkp
    Avatar
    Community Member
    42 Posts

    sorting by related fields Link to this post

    Hallo zusammen,

    my first post here on the forums. First of all, i'm really impressed with SilverStripe and all the work that you guys have put into this.. I've come quite a long way looking for a flexible (and pretty : ) CMS, and although right now i feel a little dizzy from crash-coursing myself into the system i think it'll be perfect for my next projects. So i thought a thanks would be in order here..

    My question relates to the DOM (which seems to be a great tool by the way, UncleCheese), and is somewhat related to this post
    http://www.silverstripe.org/dataobjectmanager-module-forum/show/267254#post267254
    as well as this one
    http://www.silverstripe.org/dataobjectmanager-module-forum/show/275956#post275956

    I'm trying to create an "Affiliates" Page Class which stores affiliates (for now, only name & URL) divided into categories, so much like the Resource Pages from the Tutorial. Only should the categories also come from a DataObject, so the website owner can add/remove categories dynamically.

    Here's my code so far

    ### AffiliateDataObject.php

    <?php

    class AffiliateDataObject extends DataObject {

       static $db = array (
          'Name' => 'Text',
          'Url' => 'Text'
       );

       static $has_one = array (
          'AffiliatePage' => 'AffiliatePage',
          'AffiliateCategory' => 'AffiliateCategory'
       );

       // Testing, obsolete?
       function getMyCategoryName() {
       return $this->AffiliateCategory()->Name;
       }

       function getCMSFields_forPopup() {
          $fields = new FieldSet();
          $fields->push( new TextField( 'Name', 'Name' ) );
          $fields->push( new TextField( 'Url', 'Url' ) );

          $oData = (DataObject::get('AffiliateCategory'));
          if ($oData) $CategoriesSource = $oData->toDropDownMap('ID','Name');
          $dropdown = new DropdownField('AffiliateCategoryID','Category',$CategoriesSource);
          $dropdown->setEmptyString('-- Select category --');
          $fields->push ( $dropdown );

          return $fields;
       }

    }


    ### AffiliateCategory.php

    <?php

    class AffiliateCategory extends DataObject {

       static $db = array (
          'Name' => 'Varchar(100)'
       );

       static $has_one = array (
          'AffiliatePage' => 'AffiliatePage'
       );

       static $has_many = array (
        'AffiliateDataObjects' => 'AffiliateDataObject'
       );

       public function getCMSFields_forPopup() {
    return new FieldSet(
             new TextField('Name', 'Name')
       );
    }

    }


    ### AffiliatePage.php

    <?php

    class AffiliatePage extends Page {

       static $db = array (
       );

       static $has_many = array (
          'AffiliateDataObjects' => 'AffiliateDataObject',
          'AffiliateCategories' => 'AffiliateCategory'
       );

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

          $affiliate_manager = new DataObjectManager(
             $this,
             'AffiliateDataObjects',
             'AffiliateDataObject',
             array('Name' => 'Name','Url'=>'Url','AffiliateCategory.Name'=>'Category'),
             'getCMSFields_forPopup'
          );

          $affiliate_manager->setPerPageMap(array('50'));

          $f->addFieldToTab("Root.Content.Affiliates", $affiliate_manager);

          $f->addFieldToTab("Root.Content.Categories", new DataObjectManager(
             $this,
             'AffiliateCategories',
             'AffiliateCategory',
             array('Name'=>'Name'),
             'getCMSFields_forPopup'
          ));

          return $f;

       }

    }

    class AffiliatePage_Controller extends Page_Controller {

    }

    Everything seems to be working just fine, i can add categories and get a dropdown menu showing the categories name when i add a new "Affiliate" Data Object.

    However there's no sorting by category in the DOM. If I click the header area in DOM for "Category" the loading animation appears and disappears, but no sorting occurs.

    In an ideal case, what i'm trying to achieve is alphabetcal ordering by category name (which has to be pulled from the AffiliateCategory DataObject)

    I tried changing

    ### AffiliatePage.php

    array('Name' => 'Name','Url'=>'Url','MyCategoryName'=>'Category'),

    to

    array('Name' => 'Name','Url'=>'Url','AffiliateCategory.Name'=>'Category'),

    which also outputs the categories column correctly, but renders the table header unclickable, so also no sorting.

    Could this possibly have to do with naming conventions for relationships that (while unbeknownst to me) make the whole thing work automagically?

    I've also been trying to gather more documentation about working with related field values in DOM, does anybody have some tutorial or working module recommendations for this kind of thing?

    Any help would be greatly appreciated.. Thanks and all the best,
    Andi

  • UncleCheese
    Avatar
    4085 Posts

    Re: sorting by related fields Link to this post

    Yeah, that's unsupported at the moment. I'm surprised you were able to click the column at all. Related fields should not be sortable. You should be able to add a sort clause to your DOM constructor, though. Did that not work? You'll lose the interactivity, but at least you'd have the alpha sort you want. That and a custom filter dropdown should get you pretty far along.

  • tmkp
    Avatar
    Community Member
    42 Posts

    Re: sorting by related fields Link to this post

    Hi UncleCheese,
    thanks for the quick reply!

    I'm afraid i'm not quite sure how to apply the sort clause though. Putting

    ### AffiliatePage.php

          $affiliate_manager = new DataObjectManager(
             $this,
             'AffiliateDataObjects',
             'AffiliateDataObject',
             array('Name' => 'Name','Url'=>'Url','AffiliateCategory.Name'=>'Category'),
             'getCMSFields_forPopup',
             null,
             "AffiliateCategory.Name ASC" // Sort by name
          );

    throws an error when trying to load the page in the cms. Any pointers at how to implement this?

    Thanks again!
    Andi

  • UncleCheese
    Avatar
    4085 Posts

    Re: sorting by related fields Link to this post

    What is the error?

  • UncleCheese
    Avatar
    4085 Posts

    Re: sorting by related fields Link to this post

    Oh, right.. just realized, you'll need to use the join clause, too. Make sure you're joining the category table on your object.

  • tmkp
    Avatar
    Community Member
    42 Posts

    Re: sorting by related fields Link to this post

    Hi,
    sorry, the error was an empty javascript alert. The page didn't load at all.

    Trying to get my head around the join clause, but dealing with those was basically what i've been trying to avoid all my life : )

    I'm pretty lost with this right now, going to take another look at the code tomorrow. Is there any hope that sorting by related fields will be supported anytime soon (or late)?

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