SilverStripe 如何写入国外关系的字段
SilverStripe how to write to foreign relationship's field
向国外关系字段写入表单值的最佳方式是什么?
我需要将 $coachField
的值保存到外部 table 中的特定列中。 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'
);
}
对于 1:1 关系,您可以使用 hasoneedit 模块。字段名称应该是HasOneName-_1_-FieldName
like
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 关系。
向国外关系字段写入表单值的最佳方式是什么?
我需要将 $coachField
的值保存到外部 table 中的特定列中。 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'
);
}
对于 1:1 关系,您可以使用 hasoneedit 模块。字段名称应该是HasOneName-_1_-FieldName
like
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 关系。