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.

Customising the CMS /

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

ModelAdmin and summary_fields with custom getter


Go to End
Reply


3 Posts   1448 Views

Avatar
juergr

Community Member, 17 Posts

4 January 2012 at 12:01am

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!

Avatar
swaiba

Forum Moderator, 1823 Posts

4 January 2012 at 12:46am

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

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

Avatar
juergr

Community Member, 17 Posts

4 January 2012 at 1:32am

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!