4572 Posts in 1316 Topics by 981 members
Customising the CMS
SilverStripe Forums » Customising the CMS » ModelAdmin and summary_fields with custom getter
Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba
|
Page:
1
|
Go to End | |
| Author | Topic: | 113 Views |
-
ModelAdmin and summary_fields with custom getter

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!
-
Re: ModelAdmin and summary_fields with custom getter

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
-
Re: ModelAdmin and summary_fields with custom getter

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!
| 113 Views | ||
|
Page:
1
|
Go to Top |


