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.

Data Model Questions

Mapping Name of a has_one's has_one


Reply

2 Posts   206 Views

Avatar
mhanisch

17 January 2014 at 3:50am Community Member, 5 Posts

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?

Avatar
mhanisch

17 January 2014 at 4:13am Community Member, 5 Posts

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();
      }
   }