【发布时间】:2014-06-09 02:54:44
【问题描述】:
我注意到如果模型中没有更改,模型将不会调用更新或更新事件。我猜它会将getOriginal 与getDirty 进行比较。
但是,当我只更新模型的关系时,例如,附加新类别,因为此操作存在于更新的事件中,它永远不会被触发。
在这种特定情况下,更新事件期间会删除类别,而更新事件期间会附加新类别。但只有在我更改模型中的至少一个值时才会触发它。
我错过了什么吗?
这在 4.1 中没有发生,现在在 4.2 中发生了,作曲家今天更新了。
更新:
这是我的 Product 模型启动时的 updating 和 updated 事件:
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'));
}
}
还有BaseModel中的updating事件:
static::updating(function($model)
{
if(property_exists(get_class($model), 'rules'))
{
if(!$model->validate()) return false;
}
});
这在 4.2 中不再起作用,因为当模型没有更改时,更新和更新事件将被忽略。我的修复是只使用saving 和saved 事件,在这些事件中,我可以通过检查getOriginal()是否存在来确定模型是否正在更新或创建。
【问题讨论】:
-
保存负责更新的代码。
-
@WereWolf-TheAlpha 您好,狼人先生,我已经更新了问题。