* Don't query from db in loop.

This commit is contained in:
zhujinyong
2023-06-13 08:44:37 +08:00
parent 2f9ca0e6d6
commit 154a3b8d77
3 changed files with 48 additions and 34 deletions
@@ -102,7 +102,7 @@
<div class='col-xs-7'>
<div class="progress progress-text-left">
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="<?php echo $project->hours->progress;?>" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $project->hours->progress;?>%">
<span class="progress-text"><?php echo $project->hours->progress;?>%</span>
<span class="progress-text"><?php echo $project->progress;?>%</span>
</div>
</div>
</div>
+5 -5
View File
@@ -56,13 +56,13 @@
<span class="status-project status-<?php echo $execution->status?>" title='<?php echo $statusName;?>'><?php echo $statusName;?></span>
<?php endif;?>
</td>
<td class="c-hours" title="<?php echo $execution->hours->totalEstimate . ' ' . $lang->execution->workHour;?>"><?php echo $execution->hours->totalEstimate . $lang->execution->workHourUnit;?></td>
<td class="c-hours" title="<?php echo $execution->hours->totalConsumed . ' ' . $lang->execution->workHour;?>"><?php echo $execution->hours->totalConsumed . $lang->execution->workHourUnit;?></td>
<td class="c-hours" title="<?php echo $execution->hours->totalLeft . ' ' . $lang->execution->workHour;?>"><?php echo $execution->hours->totalLeft . $lang->execution->workHourUnit;?></td>
<td class="c-hours" title="<?php echo $execution->estimate . ' ' . $lang->execution->workHour;?>"><?php echo $execution->estimate . $lang->execution->workHourUnit;?></td>
<td class="c-hours" title="<?php echo $execution->consumed . ' ' . $lang->execution->workHour;?>"><?php echo $execution->consumed . $lang->execution->workHourUnit;?></td>
<td class="c-hours" title="<?php echo $execution->left . ' ' . $lang->execution->workHour;?>"><?php echo $execution->left . $lang->execution->workHourUnit;?></td>
<?php endif;?>
<td class="c-progress">
<div class='progress-pie' data-doughnut-size='90' data-color='#3CB371' data-value='<?php echo round($execution->hours->progress);?>' data-width='24' data-height='24' data-back-color='#e8edf3'>
<div class='progress-info'><?php echo round($execution->hours->progress);?></div>
<div class='progress-pie' data-doughnut-size='90' data-color='#3CB371' data-value='<?php echo round($execution->progress);?>' data-width='24' data-height='24' data-back-color='#e8edf3'>
<div class='progress-info'><?php echo round($execution->progress);?></div>
</div>
</td>
<?php if($longBlock):?>
+42 -28
View File
@@ -236,32 +236,22 @@ class projectModel extends model
$projects = $this->loadModel('program')->getProjectList(0, $status, 0, $orderBy, $pager, 0, $involved);
if(empty($projects)) return array();
$projectIdList = array_keys($projects);
$teams = $this->dao->select('t1.root, count(t1.id) as count')->from(TABLE_TEAM)->alias('t1')
->leftJoin(TABLE_USER)->alias('t2')->on('t1.account=t2.account')
->where('t1.root')->in($projectIdList)
->andWhere('t2.deleted')->eq(0)
->groupBy('t1.root')
->fetchAll('root');
$projectParentNames = $this->getParentProgram($projects);
$estimates = $this->dao->select("t2.project as project, sum(t1.estimate) as estimate")->from(TABLE_TASK)->alias('t1')
->leftJoin(TABLE_PROJECT)->alias('t2')->on('t1.execution = t2.id')
->where('t1.parent')->lt(1)
->andWhere('t2.project')->in($projectIdList)
->andWhere('t1.deleted')->eq(0)
->andWhere('t2.deleted')->eq(0)
->groupBy('t2.project')
->fetchAll('project');
$executions = $this->dao->select('*')->from(TABLE_EXECUTION)
->where('project')->in(array_keys($projects))
->andWhere('status')->notin('done,closed')
->andWhere('deleted')->eq(0)
->orderBy($orderBy)
->fetchGroup('project');
$this->app->loadClass('pager', $static = true);
foreach($projects as $projectID => $project)
{
$orderBy = $project->model == 'waterfall' ? 'id_asc' : 'id_desc';
$pager = $project->model == 'waterfall' ? null : new pager(0, 1, 1);
$project->executions = $this->loadModel('execution')->getStatData($projectID, 'undone', 0, 0, false, '', $orderBy, $pager);
$project->teamCount = isset($teams[$projectID]) ? $teams[$projectID]->count : 0;
$project->estimate = isset($estimates[$projectID]) ? round($estimates[$projectID]->estimate, 2) : 0;
$project->parentName = $this->getParentProgram($project);
$project->executions = isset($executions[$projectID]) ? $executions[$projectID] : array();
$project->parentName = $projectParentNames[$project->id];
}
return $projects;
}
@@ -269,21 +259,45 @@ class projectModel extends model
/**
* Get all parent program of a program.
*
* @param int $parentID
* @param array $projects
* @access public
* @return string
* @return array
*/
public function getParentProgram($project)
public function getParentProgram($projects)
{
if($project->parent == 0) return '';
if(empty($projects)) return array();
$parentName = $this->dao->select('id,name')->from(TABLE_PROGRAM)->where('id')->in(trim($project->path, ','))->andWhere('grade')->lt($project->grade)->orderBy('grade asc')->fetchPairs();
$parents = array();
$projectParents = array();
foreach($projects as $project)
{
$projectParents[$project->id] = array();
$parentProgram = '';
foreach($parentName as $name) $parentProgram .= $name . '/';
$parentProgram = rtrim($parentProgram, '/');
if(!$project->parent) continue;
return $parentProgram;
foreach(explode(',', trim($project->path, ',')) as $parent)
{
if($parent == $project->id) continue;
$parents[$parent] = $parent;
$projectParents[$project->id][] = $parent;
}
}
$parentNames = $this->dao->select('id,name')->from(TABLE_PROGRAM)->where('id')->in($parents)->fetchPairs();
foreach($projectParents as $projectID => $parents)
{
$programNames = array();
foreach($parents as $parent)
{
$programNames[] = $parentNames[$parent];
}
$projectParents[$projectID] = implode('/', $programNames);
}
return $projectParents;
}
/**