我有一个名为Project的模型,它有一些像这样的模型事件设置,
class Project extends Eloquent {
public static function boot()
{
parent::boot();
// new project created
static::created(function($project)
{
// create the data informing the client to pull the new project information
$pullData = [
'id' => $project->id,
'item' => 'project',
'type' => 'created',
'interface' => '/project/basic/'.$project->id
];
foreach($project->organisation->users as $user) {
if ($user->id != $project->user_id)
Pusherer::trigger('private-user'.$user->id, 'project:createdPull', $pullData);
}
}
Event::subscribe(new ProjectEventHandler);
Event::fire('project.create', array($project));
Event::fire('project.create', array($project));
Event::forget('project.create');
Event::subscribe(new OrganisationEventHandler);
Event::fire('organisation.project.add', array($project, $project->organisation, $project->user));
Event::forget('organisation.project.add');
});
}
我创建了一个这样的项目,
$project = new Project;
$project->name = $this->first_name . "'s example project";
$project->slug = Stringhelpers::_uriHash( time() . $project->name );
$project->uri_hash = Stringhelpers::_slugify($project->name);
$project->total_cost = "1000.00";
$project->start_date = date('Y-m-d H:i:s', time());
$project->finish_date = date('Y-m-d H:i:s', strtotime('+1 month'));
//$project->owner_id = $this->id;
$project->client_id = $client->id;
$project->status = 2;
$project->user_id = $this->id;
$project->organisation_id = $organisation->id;
$project->archived_at = "0000-00-00 00:00:00";
//$project->socketId = $this->socketId;
//Add the created user to the project as a just watcher.
//$project->flushEventListeners();
if($project->save()) {
$project->users()->attach(array($this->id => array('role' => 3, 'last_tab' => 'home')));
}
在我看来,这应该在模型上触发一次创建的事件,并且每个订阅的事件一次,但是它会使事件或创建的事件被激活3次,是否有理由这样做?
=======进一步详情======
我在我的进程中保存了多个模型,我注意到我在之前的保存时会触发多个事件,所以我执行以下操作,我也得到了Item事件,
$project = new Project;
$project->name = $this->first_name . "'s example project";
$project->slug = Stringhelpers::_uriHash( time() . $project->name );
$project->uri_hash = Stringhelpers::_slugify($project->name);
$project->total_cost = "1000.00";
$project->start_date = date('Y-m-d H:i:s', time());
$project->finish_date = date('Y-m-d H:i:s', strtotime('+1 month'));
//$project->owner_id = $this->id;
$project->client_id = $client->id;
$project->status = 2;
$project->user_id = $this->id;
$project->organisation_id = $organisation->id;
$project->archived_at = "0000-00-00 00:00:00";
//$project->socketId = $this->socketId;
//Add the created user to the project as a just watcher.
//$project->flushEventListeners();
if($project->save()) {
$project->users()->attach(array($this->id => array('role' => 3, 'last_tab' => 'home')));
}
$item = new Item;
$item->name = $this->first_name . "'s example item";
$item->percentage_complete = 0;
$item->project_id = $project->id;
$item->workflow_id = 0;
$item->user_id = $this->id;
$item->start_date = $project->start_date;
$item->finish_date = $project->finish_date;
//$item->socketId = $this->socketId;
//$item->flushEventListeners();
$item->save();
我的项目模型看起来像这样,
public class Item extends Eloquent {
public static function boot() {
parent::boot();
// new item created
static::created(function($item) {
// fire project pusher events
Pusherer::trigger('project_'.$item->project_id, 'item:created', $item, $item->socketId);
Log::info("ITEM CREATE CALLBACK STARTED");
// fire off notifications and activity feeds
Event::subscribe( new ProjectEventHandler );
Event::fire('item.create', $item);
// Event::forget('item.created');
Log::info("ITEM CREATE CALLBACK FINISHED");
});
}
}
为什么每次保存时都会聚集我的事件?