Jump to:

3429 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Mapping Name of a has_one's has_one

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

Page: 1
Go to End
Author Topic: 176 Views
  • mhanisch
    Avatar
    Community Member
    3 Posts

    Mapping Name of a has_one's has_one Link to this post

    I have three classes:

    class Person ...

    private static $has_one = array(
    'Item' => 'Item'
    );
    ...

    ---
    class Item ...
    private static $has_one = array(
    'ItemType' => 'ItemType'
    );
    public static $summary_fields = array(
    'ItemType.Name'
    );
    ...

    ---
    class ItemType ...
    private static $db = array(
    'Name' => 'Varchar(255)'
    );
    ...

    ---
    and of course the backtracking $has_one's and whatnot.

    the problem: i want to show the Name of ItemType in the Dropdown of the Person class. But a mapping like this doesn't work:
    $items = Item::get()->map('ID', 'ItemType.Name');

    Any ideas how i could do that?

  • mhanisch
    Avatar
    Community Member
    3 Posts

    Re: Mapping Name of a has_one's has_one Link to this post

    for now i am using a workaround, but it is not elegant:

    on the item class i added:

    private static $db = array(
    'Name' => 'Varchar(255)'
    );
    public function onAfterWrite() {
          parent::onAfterWrite();
          
          if(!$this->Name || $this->Name = '') {
             $this->Name = $this->ItemType()->Name;
             $this->write();
          }
       }

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