2017-01-10 55 views
2

將表格值寫入外部關係領域的最佳方式是什麼?SilverStripe如何寫入外國關係的領域

我需要將$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' 
    ); 
} 

回答

2

對於1:你可以使用hasoneedit模塊1的關係。該字段的名稱應爲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關係自動神奇。

+0

工程就像一個魅力。 – Faloude