【问题标题】:Laravel 4, PHPUnit and DB::getQueryLog() always emptyLaravel 4、PHPUnit 和 DB::getQueryLog() 始终为空
【发布时间】:2015-04-30 14:15:06
【问题描述】:

我有一个 phpunit 测试,它会做一些查询,然后:

$results = $thingy::where("finder_id", "=", "37");
$queries = DB::getQueryLog();
dd($queries);

但是 $queries 总是返回一个空数组

Config::get('app.debug') 为真。

我试过了:

DB::enableQueryLog() 
DB::connection()->enableQueryLog()

没有运气。我也尝试过各种事件监听器,例如:

Event::listen("illuminate.query", function($query, $bindings, $time, $name){
    \Log::sql($query."\n");
    \Log::sql(json_encode($bindings)."\n");
});

还有什么可能导致这种情况?

编辑:为了消除 PHPUnit 的原因,我使用已知的工作模型设置了一个测试路线

Route::get('fndr',function() {
    $customer = new Customer;
    $results = $customer->first();
    var_dump($results->NAME);
    $queries = DB::getQueryLog();
    dd($queries);

});

$queries 仍然是空的。

【问题讨论】:

  • 日志记录发生在 Illuminate\Database\Connection 类中。在该类的run 方法中,调用了logQuery 方法。尝试确定日志记录是否实际被正确调用和执行。您的代码在其他方面是正确的。

标签: laravel-4 phpunit eloquent


【解决方案1】:

看来我需要指定要使用的连接名称。我假设它使用默认连接,否则。

$queries = DB::connection('name_of_connection')->getQueryLog();
dd($queries);

【讨论】:

    【解决方案2】:

    这个答案对我有帮助:

    [这里]How to get the query executed in Laravel 5 ? DB::getQueryLog returning empty array

    一些技巧 1 多个数据库连接 如果您有多个数据库连接,则必须指定要记录的连接

    为 my_connection 启用查询日志:

    DB::connection('my_connection')->enableQueryLog();
    

    获取 my_connection 的查询日志:

    print_r(
       DB::connection('my_connection')->getQueryLog()
    );
    

    enbleQueryLog()getQueryLog() 都需要指定连接名称

    【讨论】:

      猜你喜欢
      • 2018-11-04
      • 1970-01-01
      • 2015-03-01
      • 2021-07-29
      • 1970-01-01
      • 2017-07-10
      • 1970-01-01
      • 2015-12-24
      • 2016-07-22
      相关资源
      最近更新 更多