Laravel 循环中模型对象的范围
Laravel scope of model object in a 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();
...
我知道这里已经有人问过这个问题了:
但是我无法解决问题。
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();
...