Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

laravel - Determine status of multiple collection objects

I essentially have a report model. A Report has one ChartGroup and a ChartGroup can have many Charts. I essentially need to know when a report is ready to be viewed, and this can occur when at least one chart has been processed.

I then need to know when the report is ready to download, and this is when all charts for a report have been processed. This is my basic schema structure with some columns removed to reduce code.

Schema::create('reports', function (Blueprint $table) {
    $table->id();

    $table->boolean('isSubmitted')->nullable()->default(false);
    $table->boolean('isViewable')->nullable()->default(false);
    $table->boolean('reportReady')->nullable()->default(false);
});

Schema::create('chart_group', function (Blueprint $table) {
    $table->id();
    $table->foreignId('report_id');

    $table->foreign('report_id')
        ->references('id')
        ->on('reports')
        ->onDelete('cascade');
});

Schema::create('charts', function (Blueprint $table) {
    $table->id();
    $table->string('chart_name')->nullable();
    $table->boolean('chart_processed')->default(false);
    $table->foreignId('chart_group_id');

    $table->foreign('chart_group_id')
        ->references('id')
        ->on('chart_group')
        ->onDelete('cascade');
});

And then within a Laravel job, I am getting all reports and looping them. I then check the charts for a report and if it has not been processed I process it. If processed successgully, I update the chart and mark it as processed (other code).

foreach ($reports as $report) {
    foreach ($report->chartGroup->chart as $chart) {
        if (!$chart->report_processed) {
            if ($this->queryStatus($chart)) {
                if (!$report->isViewable) {
                    $report->isViewable = true;
                    $report->saveOrFail();
                }
            }
        }
    }
}

So this works fine so far, if a report has a chart that needs processing, its processed and that chart is marked as such. The problem I am having is this. A report may have 5 charts. I need a way to identify when all of the charts have been processed so I can mark the report as being ready.

How could I go about doing this?

Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well you can run another job to identify reports which all charts have been processed like below.

What I do here is just check the

available chart count for a report and then check is that count match to isViewable true count for that report.

Which means that all charts have been processed.If those counts get matched I add the report into empty collection. At last you can process $reports_to_be_downloaded collection as you required.

$reports_to_be_downloaded = collect();

foreach ($reports as $report) {
  if($report->chartGroup->chart->count() == $report->chartGroup->chart()->where('isViewable',true)->count()){
    $reports_to_be_downloaded->add($report);
  } 
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...