From c03ab269f0a7927b77900e605e80d7792ee62704 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 15 Jul 2017 10:43:22 +0200 Subject: [PATCH] Memory optimised, connect journal to tag. --- app/Import/Routine/ImportRoutine.php | 26 +++++++++++++++++++------- app/Import/Storage/ImportStorage.php | 4 ++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/app/Import/Routine/ImportRoutine.php b/app/Import/Routine/ImportRoutine.php index ee8fd22c6b..adaeab72a2 100644 --- a/app/Import/Routine/ImportRoutine.php +++ b/app/Import/Routine/ImportRoutine.php @@ -13,11 +13,11 @@ namespace FireflyIII\Import\Routine; use Carbon\Carbon; +use DB; use FireflyIII\Import\FileProcessor\FileProcessorInterface; use FireflyIII\Import\Storage\ImportStorage; use FireflyIII\Models\ImportJob; use FireflyIII\Models\Tag; -use FireflyIII\Models\TransactionJournal; use FireflyIII\Repositories\Tag\TagRepositoryInterface; use Illuminate\Support\Collection; use Log; @@ -64,14 +64,19 @@ class ImportRoutine Log::info(sprintf('Returned %d valid objects from file processor', $this->lines)); $storage = $this->storeObjects($importObjects); + Log::debug('Back in run()'); // update job: $this->job->status = 'finished'; $this->job->save(); + Log::debug('Updated job...'); + $this->journals = $storage->journals; $this->errors = $storage->errors; + Log::debug('Going to call createImportTag()'); + // create tag, link tag to all journals: $this->createImportTag(); @@ -101,7 +106,7 @@ class ImportRoutine $processor = app($class); $processor->setJob($this->job); - if ($this->job->status == 'configured') { + if ($this->job->status === 'configured') { // set job as "running"... $this->job->status = 'running'; @@ -120,6 +125,7 @@ class ImportRoutine */ private function createImportTag(): Tag { + Log::debug('Now in createImportTag()'); /** @var TagRepositoryInterface $repository */ $repository = app(TagRepositoryInterface::class); $repository->setUser($this->job->user); @@ -138,11 +144,16 @@ class ImportRoutine $this->job->extended_status = $extended; $this->job->save(); - $this->journals->each( - function (TransactionJournal $journal) use ($tag) { - $journal->tags()->save($tag); - } - ); + Log::debug(sprintf('Created tag #%d ("%s")', $tag->id, $tag->tag)); + Log::debug('Looping journals...'); + $journalIds = $this->journals->pluck('id')->toArray(); + $tagId = $tag->id; + foreach ($journalIds as $journalId) { + Log::debug(sprintf('Linking journal #%d to tag #%d...', $journalId, $tagId)); + DB::table('tag_transaction_journal')->insert(['transaction_journal_id' => $journalId, 'tag_id' => $tagId]); + } + Log::debug('Done!'); + Log::info(sprintf('Linked %d journals to tag #%d ("%s")', $this->journals->count(), $tag->id, $tag->tag)); return $tag; @@ -160,6 +171,7 @@ class ImportRoutine $storage->setDateFormat($this->job->configuration['date-format']); $storage->setObjects($objects); $storage->store(); + Log::info('Back in storeObjects()'); return $storage; } diff --git a/app/Import/Storage/ImportStorage.php b/app/Import/Storage/ImportStorage.php index c3d0723cdf..964fafa1f7 100644 --- a/app/Import/Storage/ImportStorage.php +++ b/app/Import/Storage/ImportStorage.php @@ -116,8 +116,9 @@ class ImportStorage Log::error(sprintf('Cannot import row #%d because: %s', $index, $e->getMessage())); } } + Log::info('ImportStorage has finished.'); - + return true; } /** @@ -383,7 +384,6 @@ class ImportStorage // run rules: $this->applyRules($journal); $this->job->addStepsDone(1); - $this->journals->push($journal); Log::info(