Jump to:

5554 Posts in 1742 Topics by 1227 members

Customising the CMS

SilverStripe Forums » Customising the CMS » ModelAdmin and summary_fields with custom getter

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

Page: 1
Go to End
Author Topic: 1174 Views
  • juergr
    Avatar
    Community Member
    17 Posts

    ModelAdmin and summary_fields with custom getter Link to this post

    I have a class wich looks like this (simplified):

    class Tour extends DataObject implements PermissionProvider {
    static $default_sort = 'StartDatum ASC';

    static $db = array(
    'TourName' => 'Varchar(255)',
    'Typ' => "Enum('Kurs, Skitour, Bergtour, Hochtour, Klettertour, Wanderung, Velotour, Anderes', 'Anderes') ",
    'Beschreibung' => 'Text',
    'StartDatum' => 'Date',
    'EndDatum' => 'Date',
    'Tourenbericht' => 'HTMLText'
    );

    static $summary_fields = array(
    'Datum' => 'Datum',
    'TourName' => 'Tour',
    'Typ' => 'Art',
    'TourenberichtSummary' => 'Tourenbericht'
    );

    public function getDatum() {
    $startDatum = new SwissDate(); // SwissDate overrides the RangeString function of Date
    $startDatum->setValue($this->StartDatum);
    $endDatum = new SwissDate();
    $endDatum->setValue($this->EndDatum);
    if($startDatum->Format('d.m.Y') == $endDatum->Format('d.m.Y')) {
    return $startDatum->Format('d.m.Y');
    }
    return $startDatum->RangeString($endDatum);
    }

    public function getTourenberichtSummary() {
    if($this->Tourenbericht) {
    return '<img src="cms/images/alert-good.gif" />';
    }
    return '';
    }
    }

    and a ModelAdmin for this which looks like this:

    class TourAdmin extends ModelAdmin {
    static $managed_models = array(
    'Tour'
    );

    static $url_segment = 'touren';
    static $menu_title = 'Touren';
    }

    but when i call the AdminInterface (localhost/admin/touren) iget this error:
    [User Error] Uncaught Exception: Unable to traverse to related object field [Datum] on [Tour]

    How do I use custom getters in ModelAdmin? At all other places this works just fine with the custom getters!

  • swaiba
    Avatar
    Forum Moderator
    1796 Posts

    Re: ModelAdmin and summary_fields with custom getter Link to this post

    I think you are missing the casting that tells silverstripe to use your function...

    http://doc.silverstripe.org/sapphire/en/topics/datamodel#casting

  • juergr
    Avatar
    Community Member
    17 Posts

    Re: ModelAdmin and summary_fields with custom getter Link to this post

    OK, if I just cast all custom getters to 'HTMLVarchar' as they just returns a string wich may contain HTML it's working fine.

    Thanks for your help!

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