Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Bug renaming DataObject SummaryFields?

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

Page: 1
Go to End
Author Topic: 207 Views
  • TheGuy
    Avatar
    Community Member
    2 Posts

    Bug renaming DataObject SummaryFields? Link to this post

    Hi,

    I have extended DataObject and was trying to get it to display nice headers in the CMS Overview. According to the documentation it's supposed to be as simple as specifying an associative array. However if I do it like it's done in the documentation the "nice names" don't show up but instead it will display the actual field names as headers in the CMS.
    When I tried to add another column (summary field) from another Object from a has_one relationship and thus used the dot notation mentioned and demonstrated in the documentation it displayed the correct nice name for that column and the correct data, however all the other columns were suddenly empty.

    The Code:

    class MyObj extends DataObject {
       private static $db = array(
          'dttm' => 'SS_DateTime',
          'name' => 'varchar(255)',
          'location' => 'varchar(255)',
       );
       private static $has_one = array('Page' => 'Page', 'Category' => 'MyCategory');
       //private static $summary_fields = array('dttm' => 'Date', 'name' => 'Name', 'location' => 'Where'); // Does display content for all columns but only shows raw field names as column headers instead of the nice name.
       //private static $summary_fields = array('dttm' => 'Date', 'name' => 'Name', 'location' => 'Where', 'Category.short' => 'Type'); // According to Documentation this should work. Doesn't display any content but Type, has raw field name correct column headers for all but Type.
       //private static $summary_fields = array('dttm.Nice' => 'Date', 'name' => 'Name', 'location' => 'Where', 'Category.short' => 'Type'); // According to documentation this should work. Shows no data but Type and Date, has correct column headers for Date and Type ONLY.
       //private static $summary_fields = array('dttm.Nice' => 'Date', 'name.Summary' => 'Name', 'location.Summary' => 'Where', 'Category.short' => 'Type'); // Does display content for Date and Type only, has correct column headers for all columns. No Summary on varchar, so not displaying data is to be expected I guess.
       private static $summary_fields = array('dttm.Nice' => 'Date', 'name.getValue' => 'Name', 'location.getValue' => 'Where', 'Category.short' => 'Type'); // Finally displays correct Data and column headers for all columns. Guessing the outcome / trying random functions on varchar worked at last.

    }

    class MyCategory extends DataObject {
       private static $db = array(
          'name' => 'varchar(60)',
          'short' => 'varchar(4)'
       );
       private static $has_one = array('Page' => 'Page');
       private static $summary_fields = array('name', 'short');
    }

    So it seems that nice column headers only work when you are using dot notation for the summary fields (which is quite a pain and superfluous). I don't necessarily need or want to display nice dates, not to mention having to call a function for each one adds unnecessary overhead.
    All of this leads me to believe that there is a Bug, or at the very least undesirable behavior that should be fixed to match the documentation, which makes sense.
    All of this has been done on SilverStripe 3.1.2

    Even though unrelated, another thing I noticed is that I had to run /dev/build each time I changed the summary_fields in order to see any changes in the CMS in the first place, which seems weird considering that shouldn't cause any modifications to the database (nor were any indicated on the /dev/build output). It's just PHP Code which as far as I can tell gets executed every time the cms page is loaded so there should be no need to run /dev/build for these kind of changes to take effect.

  • martimiz
    Avatar
    Forum Moderator
    1037 Posts

    Re: Bug renaming DataObject SummaryFields? Link to this post

    This is indeed a bit strange! In the following example (after doing a ?flush=all, of course ):

    private static $summary_fields = array(
       'Name' => 'Your name',
       'Product.Title' => 'Your product'
    );

    The headers will read 'Name' and 'Your product'. For dotted notation it seems to work, for simple fields it doesn't. Would that be by design? Maybe raise an issue on Github...

    I normaly use the fieldLabels function to get all my labels/headers. It's more flexible, as you can use it for all your objects fields, and localize them as well:

    public function fieldLabels($includerelations = true) {
          $labels = parent::fieldLabels(true);

          $labels['Name'] = _t('MyObject.NAME', 'Name');
          $labels['PageID'] = _t('MyObject.PAGE', 'Page');
          $labels['Product.Title'] = _t('MyObject.PRODUCT', 'Product title');

          return $labels;
       }

  • TheGuy
    Avatar
    Community Member
    2 Posts

    Re: Bug renaming DataObject SummaryFields? Link to this post

    I will have to look in to that fieldLabels function and make some tests and see how that works. Thanks for the suggestion. Kind of hard to know all the options and possibilities
    Guess it is time to create a GitHub Account to raise an issue there...

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