循环中模型对象的Laravel范围



我知道这里已经问过这个问题:Laravel Query Scope in Loop

但我无法解决这个问题。

foreach ($activePortalProviders as $eachProvider) {
$stockModel = new Stock();
$response = array();
$stockList = array();
$portalProviderID = $eachProvider['portalProviderPID'];
$getAllStocks = $stockModel->getAllStockBaseOnProviderID($portalProviderID)->select($selectedColumn)->get();
if (count($getAllStocks) > 0) {
foreach ($getAllStocks as $eachStock) {
$gameModel = new Game();
//To find the game related to that particular stock and provider id
$gameStatus = [1, 2]; // open/closed games should be listed
$currentTimeStamp = microtimeToDateTime(getCurrentTimeStamp());
$portalProviderPID = $eachStock['portalProviderPID'];
DB::connection()->enableQueryLog();
$queries = null;
Log::debug('--before--'. microtimeToDateTime(getCurrentTimeStamp(), true));
$gameData = $gameModel->getAllProviderGamesByStock($portalProviderPID, $eachStock['stockUUID'], $gameStatus, $currentTimeStamp);
$queries = DB::getQueryLog();
Log::debug($queries);
Log::debug('--after--'. microtimeToDateTime(getCurrentTimeStamp(), true));
//other code..........
unset($gameModel);
}
//other code..........
}
unset($stockModel);
} 

尝试创建单独的对象并扰乱它们

尝试使用(clone $gameModel)->getAllProviderGamesByStock(---Params--)

但是我得到的查询日志,每次都不断添加旧的查询,比如:

[2020-04-15 12:03:46] local.DEBUG: --before--2020-04-15 12:03:46.2226  
[2020-04-15 12:03:46] local.DEBUG: array (
0 => 
array (
'query' => '---qry--',
'bindings' => 
array (
0 => 'active',
1 => 2,
2 => '7b6c89f3-fcef-47b3-b4d4-ce550e0b56ed',
3 => 1,
4 => 2,
),
'time' => 2.22,
),
)  
[2020-04-15 12:03:46] local.DEBUG: --after--2020-04-15 12:03:46.2403  
[2020-04-15 12:03:46] local.DEBUG: --before--2020-04-15 12:03:46.2412  
[2020-04-15 12:03:46] local.DEBUG: array (
0 => 
array (
'query' => '---qry--',
'bindings' => 
array (
0 => 'active',
1 => 2,
2 => '7b6c89f3-fcef-47b3-b4d4-ce550e0b56ed',
3 => 1,
4 => 2,
),
'time' => 2.22,
),
1 => 
array (
'query' => '---qry--',
'bindings' => 
array (
0 => 'active',
1 => 2,
2 => 'ebac522c-24ae-4ba5-8bc9-1c32641ab08f',
3 => 1,
4 => 2,
),
'time' => 1.83,
),
)  
[2020-04-15 12:03:46] local.DEBUG: --after--2020-04-15 12:03:46.2454  
[2020-04-15 12:03:46] local.DEBUG: --before--2020-04-15 12:03:46.2462  
[2020-04-15 12:03:46] local.DEBUG: array (
0 => 
array (
'query' => '---qry--',
'bindings' => 
array (
0 => 'active',
1 => 2,
2 => '7b6c89f3-fcef-47b3-b4d4-ce550e0b56ed',
3 => 1,
4 => 2,
),
'time' => 2.22,
),
1 => 
array (
'query' => '---qry--',
'bindings' => 
array (
0 => 'active',
1 => 2,
2 => 'ebac522c-24ae-4ba5-8bc9-1c32641ab08f',
3 => 1,
4 => 2,
),
'time' => 1.83,
),
2 => 
array (
'query' => '---qry--',
'bindings' => 
array (
0 => 'active',
1 => 2,
2 => '01fbd201-2837-4589-b60d-7321f319cd72',
3 => 1,
4 => 2,
),
'time' => 1.82,
),
)  
[2020-04-15 12:03:46] local.DEBUG: --after--2020-04-15 12:03:46.2503  

而且这种情况还在持续增长。

还有其他人面临类似的问题吗?

在我看来,你的函数代码很好,但你使用查询日志的方式会误导你自己,因为你从未刷新过日志

尝试像这样刷新查询日志:

...
$queries = DB::getQueryLog();
DB::flushQueryLog();
...

最新更新