将表格价值写入外国关系领域的最佳方法是什么?
我需要将$coachField
的值保存到外表中的特定列中。 IE:在Team
对象表单中,我需要能够保存Coach
的名称(它与记录具有一对一的关系)。
我倾向于使用Team
中的 onAfterWrite 来获取教练的名字,但我不确定如何检索教练的价值。第一名,尤其是如果这是最好的方法。
当前数据对象
class Team extends DataObject {
// The value needs to be saved in Coach->Name
private static $has_one = array(
'Coach' => 'Coach'
);
public function getCMSFields() {
// The form field where to get the value from
$coachField = TextField::create('CoachName', 'Who is the coach');
}
}
外国数据对象
class Coach extends DataObject {
// Here's where the name should be written to
private static $db = array(
'Name' => 'Varchar'
);
private static $belongs_to = array(
'Team' => 'Team'
);
}
答案 0 :(得分:2)
对于1:1关系,您可以使用hasoneedit模块。该字段的名称应为HasOneName-_1_-FieldName
,如
class Team extends DataObject {
// The value needs to be saved in Coach->Name
private static $has_one = array(
'Coach' => 'Coach'
);
public function getCMSFields() {
$fields = parent::getCMSFields(); //scaffold all fields
// The form field where to get the value from
$fields->addFieldsToTab('Root.Main', TextField::create('Coach-_1_-Name', 'Who is the coach');
return $fields;
}
}
它会自动保存到has_one关系。