2014-07-04 40 views
1

我想執行onAfterWrite或之前或onBeforeDelete一個簡單的寫入命令。但它根本不起作用。銀條紋3.1 - onBefore /之後刪除/寫入不起作用

Object1 - >這裏我想執行代碼。與Object2具有$ has_one關係(Item)

public function onAfterWrite(){ 
    parent::onAfterWrite(); 

    $item = Object2::get()->byID($this->ItemID); 
    $item->Title = 'test123'; 
    $item->write(); 
} 

相同的問題在對方onAfter/Before函數中。 如果沒有錯誤,或其他。

錯誤在哪裏?

+0

我們能看你怎麼定義你2間的關係?另外,你究竟想用這個來達到什麼目的? – colymba

+0

如果你的關係被定義了,Item1> Object2' – colymba

+0

問題是我在Object1上使用內聯網格,那麼獲取Object2的一個簡單方法是'$ item = $ this-> Item()'。所以看起來如果你編輯smthg。在內聯網格中,第一次保存不保存object1。如果你再次點擊保存按鈕,它會工作.. – invictus

回答

1

如果我正確理解你,你想獲得和操縱通過has_one關係與你的Object1記錄相關的Object2記錄。假設你已經宣佈你的對象2這樣的關係:

class Object2 extends DataObject{ 
    private static $has_one = array(
    'Object1' => 'Object1' 
); 
    ... 

在Object1你onAfterWrite代碼看起來應該像

public function onAfterWrite(){ 
    parent::onAfterWrite(); 
    // use the find() method to look up the relation 
    $item = Object2::get()->find('Object1ID', $this->ItemID); 
    // check that the related item exists before editing 
    if($item){ 
     $item->Title = 'test123'; 
     $item->write(); 
    } 
} 
+0

這並不能解決我的問題,但它解決了這個問題;)我發現我在object1上使用的內聯網格會產生一些問題。 – invictus