Jump to:

3375 Posts in 999 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » has_many relation in summary fields

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

Page: 1
Go to End
Author Topic: 276 Views
  • Phils
    Avatar
    Community Member
    5 Posts

    has_many relation in summary fields Link to this post

    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

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: has_many relation in summary fields Link to this post

    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
    }

  • Phils
    Avatar
    Community Member
    5 Posts

    Re: has_many relation in summary fields Link to this post

    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.

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