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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Data Model Questions /

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

Mapping Name of a has_one's has_one


Go to End


2 Posts   724 Views

Avatar
mhanisch

Community Member, 5 Posts

17 January 2014 at 3:50am

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

Community Member, 5 Posts

17 January 2014 at 4:13am

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