Skip to content

Commit

Permalink
[import] binary comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
rastislav-chynoransky authored and eronisko committed Sep 29, 2023
1 parent 14bbfed commit e2204d9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
14 changes: 11 additions & 3 deletions app/Harvest/Importers/AbstractImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Facades\DB;

abstract class AbstractImporter
{
Expand Down Expand Up @@ -109,8 +110,11 @@ protected function processHasMany(Model $model, $field, array $relatedRows, $all
foreach ($relatedRows as $relatedRow) {
$data = $this->mappers[$field]->map($relatedRow);
$conditions = $this->getConditions($field, $data);
$instance = $model->$field()->firstOrNew($conditions);
$instance->forceFill($data);
$query = $model->$field();
foreach ($conditions as $column => $value) {
$query->where(DB::raw("BINARY $column"), $value);
}
$instance = $query->firstOrNew()->forceFill($data);
$instance->save();
$updateIds[] = $instance->getKey();
}
Expand Down Expand Up @@ -139,7 +143,11 @@ protected function processBelongsToMany(Model $model, $field, array $relatedRows
foreach ($relatedRows as $relatedRow) {
$data = $this->mappers[$field]->map($relatedRow);
$conditions = $this->getConditions($field, $data);
$relatedModel = $relatedModelClass::where($conditions)->first();
$query = $relatedModelClass::query();
foreach ($conditions as $column => $value) {
$query->where(DB::raw("BINARY $column"), $value);
}
$relatedModel = $query->first();

if (!$relatedModel && !$allowCreate) {
continue;
Expand Down
18 changes: 18 additions & 0 deletions tests/Harvest/Importers/AuthorityImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,22 @@ public function testRelatedButNotExisting()
$authority = $this->importer->import($row, new Progress());
$this->assertEquals(0, $authority->relationships->count());
}

public function testAccentSensitivity()
{
$row = FakeRecordFactory::buildAuthority([
'names' => [
[
'name' => ['Mařák, Július'],
'prefered' => [false],
],
[
'name' => ['Marak, Julius'],
'prefered' => [false],
],
],
]);
$authority = $this->importer->import($row, new Progress());
$this->assertCount(2, $authority->names);
}
}

0 comments on commit e2204d9

Please sign in to comment.