Skip to content

Commit

Permalink
Small fixes for phpleague CSB reader v9
Browse files Browse the repository at this point in the history
  • Loading branch information
snipe committed Jun 4, 2019
1 parent 765417c commit 27231d4
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions app/Console/Commands/ImportLocations.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ public function handle()
$filename = $this->argument('filename');
$csv = Reader::createFromPath(storage_path('private_uploads/imports/').$filename, 'r');
$this->info('Attempting to process: '.storage_path('private_uploads/imports/').$filename);
$csv->setOffset(1); //because we don't want to insert the header
$csv->setHeaderOffset(0); //because we don't want to insert the header
$results = $csv->getRecords();

// Import parent location names first if they don't exist
foreach ($results as $parent_index => $parent_row) {

$parent_name = trim($parent_row['Parent Name']);
$this->info('- Parent: '.$parent_name.' in row as: '.trim($parent_row['Parent Name']));
// First create any parents if they don't exist

if ($parent_name!='') {
Expand All @@ -78,10 +78,12 @@ public function handle()

}

$this->info('----- Parents Created.... backfilling additional details... --------');
// Loop through ALL records and add/update them if there are additional fields
// besides name
foreach ($results as $index => $row) {

$parent_name = trim($row['Parent Name']);
// Set the location attributes to save
$location = Location::firstOrNew(array('name' => trim($row['Name'])));
$location->name = trim($row['Name']);
Expand All @@ -96,12 +98,13 @@ public function handle()

$this->info('Checking location: '.$location->name);

// If a parent name nis provided, we created it earlier in the script,
// If a parent name is provided, we created it earlier in the script,
// so let's grab that ID
if ($parent_name) {
$this->info('-- Searching for Parent Name: '.$parent_name.' - '.$row['Parent Name']);
$parent = Location::where('name', '=', $parent_name)->first();
$location->parent_id = $parent->id;
$this->info('Parent ID: '.$parent->id);
$this->info('Parent: '.$parent_name.' - ID: '.$parent->id);
}

// Make sure the more advanced (non-name) fields pass validation
Expand Down

0 comments on commit 27231d4

Please sign in to comment.