我想在後臺添加相關模型時調用函數。 這可以通過使用樞軸模型來實現,但在這種情況下必須有一些樞軸數據,否則樞軸模型事件不會被觸發。如何聆聽關係添加事件?
我不需要任何數據透視數據,只是爲了聽添加關係的事件。
我想在後臺添加相關模型時調用函數。 這可以通過使用樞軸模型來實現,但在這種情況下必須有一些樞軸數據,否則樞軸模型事件不會被觸發。如何聆聽關係添加事件?
我不需要任何數據透視數據,只是爲了聽添加關係的事件。
那麼你問的是複雜的,而不是輕易可行的。
如果你看看關係管理器行爲函數onRelationManageCreate,你可以看到它不會觸發任何事件。
我建議你擴展行爲,用你自己的方法覆蓋onRelationManageCreate()方法,並用你自己的行爲代替relationmanager行爲。
public MyRelationController extends RelationController
{
public function afterRelationCreate()
{
}
public function onRelationManageCreate()
{
parent::onRelationManageCreate();
$this->afterRelationCreate();
}
}
這當然沒有關聯鏈接記錄,下拉選擇,關係形式,複選框列表。
如果你真的想抓住它,你需要聽取創建的模型的onCreate方法。
我想這會爲「屬於關聯」關係只工作
我覺得關係補充說,但它不是直接保存在數據庫,因爲它會被存儲在表中遞延第一,所以我們可以攔截它。
所以如果你可以聽的 「DeferredBinding」 型號表事件,你可以達到你想要的。
在DeferredBinding表它擁有所有的相關信息:
Schema::create('deferred_bindings', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('master_type')->index();
$table->string('master_field')->index();
$table->string('slave_type')->index();
$table->string('slave_id')->index();
$table->string('session_key');
$table->boolean('is_bind')->default(true);
$table->timestamps();
});
,你可以看到你可以得到大量的信息在那裏。
use October\Rain\Database\Models\DeferredBinding as DeferredBindingModel;
使用這種模式,那麼:
DeferredBindingModel::saved(function($model) {
// you can check relations etc if your parent model is foo
// then you can check for which model this data is sotred
// $model->master_type it will be you model's full name space
// you can compare it
if($model->master_type == 'foo') {
// your interception code here
}
}
請讓我們知道,如果它是幫助已滿:)。
當我用我自己的方式擴展RelationController類時,我得到一個豁免: 'MyClass中使用的關係行爲沒有定義模型。' 我錯過了什麼? – dragontree
您是否添加了$ relationConfig? – Tschallacka
是的。我剛剛在實現數組中使用了我的自定義行爲類。 – dragontree