2014-06-08 159 views
0

我注意到如果模型中沒有更改,模型將不會調用更新或更新的事件。我想它會比較getOriginalgetDirty強制觸發更新+更新事件

但是,當我僅更新模型的關係時(例如,附加新類別),由於此操作位於更新的事件內部,因此永遠不會觸發它。

在這種特定情況下,類別在更新事件期間被丟棄,並且在更新事件期間附加新類別。但只有在模型中至少更改一個值時纔會觸發它。

我錯過了什麼嗎?

4.1中沒有發生這種情況,現在發生在4.2,作曲家今天更新了。

更新:

下面是我的產品型號的開機updatingupdated事件:

public static function boot() 
{ 
    parent::boot(); 

    static::updating(function($model) 
    { 
     $model->author_id = 1;//Auth::user()->id; 

     $history = new ProductHistory($model->getOriginal());    
     $model->history()->save($history); 

     $model->languages()->update(array(Product::$historyKey => $history->id)); 
     $model->currencies()->update(array(Product::$historyKey => $history->id)); 
     $model->paymentTypes()->update(array(Product::$historyKey => $history->id)); 
    } 

    static::updated(function($model) 
    { 
     if(Input::has('languages')) $model->languages()->attach(Input::get('languages')); 
     if(Input::has('currencies')) $model->currencies()->attach(Input::get('currencies')); 
     if(Input::has('payment_types')) $model->paymentTypes()->attach(Input::get('payment_types')); 
    } 
} 

而且updating事件在BaseModel

static::updating(function($model) 
    { 
     if(property_exists(get_class($model), 'rules')) 
     {    
      if(!$model->validate()) return false; 
     } 
    }); 

這在4.2中不再起作用,因爲更新和u當模型沒有變化時,忽略pdated事件。我的修復程序只能使用savingsaved事件,在這些事件中,我可以通過檢查是否更新或創建模型來確定是否存在getOriginal()事件。

+0

st你負責更新的代碼。 –

+0

@ WereWolf-TheAlpha嗨,狼人先生,我已經更新了這個問題。 – user2094178

回答

0

觸發父模型的update只是添加$touches財產子模型,例如,如果你正在更新的ProductHistory模型,它是另一種模式的孩子則只需在您的ProductHistory模型添加以下屬性的最簡單方法:

// Add this in ProductHistory model 
protected $touches = array('Product'); 

現在每次你更新ProductHistory這也將更新updated_at場在它的parant模式,在這種情況下,它Product模型,所以updated_at字段將被更新,更新後的事件將火勢太大,這讓感覺更新updated_at親父模型在子模型更新時的性質。

檢查Touching Parent TimestampsLaravel網站。

+0

這看起來不錯,先生,但在這種情況下,ProductHistory正在更新事件內部創建。不應該'getOriginal()'返回模型的關係? – user2094178