From b94070d34a7a5ff953b41a7f92da93565a87b8b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Pavez?= Date: Mon, 21 Aug 2023 12:54:51 -0400 Subject: [PATCH] - Clear model cache when using upsert - Testing for upsert with memory cache --- src/Database/Builder.php | 2 + src/Database/QueryBuilder.php | 2 + tests/Database/ModelTest.php | 71 +++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) diff --git a/src/Database/Builder.php b/src/Database/Builder.php index 2d760194d..f42dcf2a2 100644 --- a/src/Database/Builder.php +++ b/src/Database/Builder.php @@ -222,6 +222,8 @@ public function upsert(array $values, $uniqueBy, $update = null) return 0; } + $this->clearDuplicateCache(); + if (!is_array(reset($values))) { $values = [$values]; } diff --git a/src/Database/QueryBuilder.php b/src/Database/QueryBuilder.php index 9126576ad..14289c59c 100644 --- a/src/Database/QueryBuilder.php +++ b/src/Database/QueryBuilder.php @@ -311,6 +311,8 @@ public function upsert(array $values, $uniqueBy, $update = null) return 0; } + $this->clearDuplicateCache(); + if ($update === []) { return (int) $this->insert($values); } diff --git a/tests/Database/ModelTest.php b/tests/Database/ModelTest.php index f704f7e16..b2a4b8b2a 100644 --- a/tests/Database/ModelTest.php +++ b/tests/Database/ModelTest.php @@ -132,6 +132,63 @@ public function testHiddenAttributes() $this->assertArrayNotHasKey('description', $model->toArray()); } + public function testUpsert() + { + $this->getBuilder()->create('test_model2', function ($table) { + $table->increments('id'); + $table->string('name')->nullable(); + $table->timestamps(); + }); + + $this->getBuilder()->create('test_model_middle', function ($table) { + $table->increments('id'); + $table->string('value')->nullable(); + $table->timestamps(); + + $table->integer('model1_id')->unsigned(); + $table->integer('model2_id')->unsigned(); + + $table->foreign('model1_id')->references('id')->on('test_model1'); + $table->foreign('model2_id')->references('id')->on('test_model2'); + $table->unique(['model1_id', 'model2_id']); + }); + + $model1Row = TestModelGuarded::create([ + 'name' => 'Row 1', + 'data' => 'Test data' + ]); + + $model2Row = TestModel2::create([ + 'name' => 'Test', + ]); + + $test3Row = TestModelMiddle::create([ + 'model1_id' => $model1Row->id, + 'model2_id' => $model2Row->id, + 'value' => '1' + ]); + + TestModelMiddle::upsert([ + 'model1_id' => $model1Row->id, + 'model2_id' => $model2Row->id, + 'value' => '1' + ], ['model1_id', 'model2_id'], ['value']); + + $modelMiddleRow = TestModelMiddle::first(); + + $this->assertEquals('1', $modelMiddleRow->value); + + TestModelMiddle::upsert([ + 'model1_id' => $model1Row->id, + 'model2_id' => $model2Row->id, + 'value' => '2' + ], ['model1_id', 'model2_id'], ['value']); + + $modelMiddleRow = TestModelMiddle::first(); + + $this->assertEquals('2', $modelMiddleRow->value); + } + protected function createTable() { $this->getBuilder()->create('test_model', function ($table) { @@ -199,3 +256,17 @@ class TestModelHidden extends Model public $table = 'test_model'; } + +class TestModel2 extends Model +{ + protected $guarded = []; + + public $table = 'test_model2'; +} + +class TestModelMiddle extends Model +{ + protected $guarded = []; + + public $table = 'test_model_middle'; +}