Php SilverStripe如何写对外关系';s场

Php SilverStripe如何写对外关系';s场,php,orm,silverstripe,Php,Orm,Silverstripe,将表单值写入外部关系字段的最佳方式是什么? 我需要将$coachField的值保存到外部表中的特定列中。IE:在团队对象表单中,我需要能够保存教练的姓名(该姓名与记录有一一对应关系) 我倾向于在Team中使用onAfterWrite来获取教练的名字,但我不确定首先如何检索值,最重要的是,这是否是最好的方法 当前数据对象 class Team extends DataObject { // The value needs to be saved in Coach->Name

将表单值写入外部关系字段的最佳方式是什么?

我需要将
$coachField
的值保存到外部表中的特定列中。IE:在
团队
对象表单中,我需要能够保存
教练
的姓名(该姓名与记录有一一对应关系)

我倾向于在
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'
    );
}
外部数据对象

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关系,可以使用模块。字段的名称应为
HasOneName-\u 1\u-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关系。

就像一个符咒。