Php Laravel 4中的交易和事件

Php Laravel 4中的交易和事件,php,events,laravel,laravel-4,event-listener,Php,Events,Laravel,Laravel 4,Event Listener,当从Laravel的closure事务函数中触发事件时,事件中的数据库操作是否也是事务的一部分,还是在事务之外 Snippet 1 Event::listen('fireme',function($data){ User::where('votes', '>', 100)->update(array('status' => 2)); }); Snippet 2 DB::transaction(function(){

当从Laravel的closure事务函数中触发事件时,事件中的数据库操作是否也是事务的一部分,还是在事务之外

Snippet 1
    Event::listen('fireme',function($data){
         User::where('votes', '>', 100)->update(array('status' => 2));
    });

Snippet 2
    DB::transaction(function(){
            User::where('votes', '>', 100)->update(array('email' => 'something@somewebsite.com'));
            Event::fire('fireme',array('email' => 'something@somewebsite.com'));
    });

代码段1是否属于代码段2上定义的事务?

我有完全相同的问题

按照@alexandre danault的建议,我可以确认事件处理程序中抛出的异常(事件从事务中触发)将导致事务回滚。我想把这个答案贴出来也许可以让你不用先运行自己的测试。以下是我的代码片段(我没有使用事务的闭包形式):


如果您只有一个数据库连接,并且您在一个事务中,那么您在数据库中所做的一切都必须是该事务的一部分。Laravel不会为事件打开额外的数据库连接。

测试这一点听起来很简单,只需在侦听器中引发en exception,然后查看事务的其余部分是回滚还是提交。
// I throw an exception from within this event handler
Event::listen('transaction.statusChange', 'TransactionHandler@onStatusChange');

// Here's the transaction that fires the event
DB::beginTransaction();
try {
    $this->status = $status;
    if (!$this->save()){
        throw new Exception('...');
    }
    Event::fire('transaction.statusChange', ['transaction' => $this, 'status' => $status]);
} catch(Exception $e){
    DB::rollback();
    // Log exception

    return false;
}
DB::commit();