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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

has_many relation in summary fields


Go to End


3 Posts   2603 Views

Avatar
Phils

Community Member, 11 Posts

26 November 2013 at 11:14pm

Hi everybody,
I was trying to get a has_many relation into the table of a Model Admin by summary fields. Is there a way to do that?
By now I got:

<?php
class Client extends DataObject {

static $has_many = array(
"Domains" => "Domain"
);

static $summary_fields = array(
'Domains.DomainName' => 'Domains'
);

AND

<?php
class Domain extends DataObject {
static $db = array(
'DomainName' => "Varchar"
);

which should work with has_one relations but doesn't with has_many.
I would be happy for every hint.

Thanks,
Phil

Avatar
martimiz

Forum Moderator, 1391 Posts

5 December 2013 at 1:22am

Edited: 05/12/2013 1:23am

i don't think you can do that with a Has_many relation. i mean: which of the (many) records would you display in the table? One way to overcome this would be to use a function, and let it return a comma-separated list or something like that:

static $summary_fields = array(
    'showDomains' => 'Domains'
)

protected function showDomains() {
  // return some string value
}

Avatar
Phils

Community Member, 11 Posts

11 January 2014 at 12:40am

Thanx martimiz,
with your suggestion I did it like this:

______________________________________________________________________________________

static $summary_fields = array(
'showDomains' => 'Domains'
);

protected function showDomains() {
$displayDomains = array();
$domains = $this->Domains();
foreach ($domains as $sc) {
$displayDomains[$sc->ID] = $sc->Domainname;
}
$stringDomains = implode(",", $displayDomains);
return $stringDomains;
}
______________________________________________________________________________________

Don't know if it's nice cause of my missing php skills, but it works.