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

Mapping Name of a has_one's has_one


Go to End


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