diff --git a/src/Handlers/DatabaseHandler.php b/src/Handlers/DatabaseHandler.php index fd38378..bf67202 100644 --- a/src/Handlers/DatabaseHandler.php +++ b/src/Handlers/DatabaseHandler.php @@ -99,6 +99,7 @@ public function set(string $class, string $property, $value = null, ?string $con $result = db_connect()->table($this->table) ->where('class', $class) ->where('key', $property) + ->where('context', $context) ->update([ 'value' => $value, 'type' => $type, diff --git a/tests/SettingsTest.php b/tests/SettingsTest.php index 259d690..bcdc36a 100644 --- a/tests/SettingsTest.php +++ b/tests/SettingsTest.php @@ -258,4 +258,40 @@ public function testForgetWithContext() $this->assertSame('Bar', $settings->get('Test.siteName', 'category:disease')); } + + /** + * @see https://github.com/codeigniter4/settings/issues/20 + */ + public function testSetUpdatesContextOnly() + { + $settings = new Settings(config('Settings')); + + $settings->set('Test.siteName', 'Humpty'); + $settings->set('Test.siteName', 'Jack', 'context:male'); + $settings->set('Test.siteName', 'Jill', 'context:female'); + $settings->set('Test.siteName', 'Jane', 'context:female'); + + $this->seeInDatabase($this->table, [ + 'class' => 'Tests\Support\Config\Test', + 'key' => 'siteName', + 'value' => 'Jane', + 'type' => 'string', + 'context' => 'context:female', + ]); + + $this->seeInDatabase($this->table, [ + 'class' => 'Tests\Support\Config\Test', + 'key' => 'siteName', + 'value' => 'Humpty', + 'type' => 'string', + 'context' => null, + ]); + $this->seeInDatabase($this->table, [ + 'class' => 'Tests\Support\Config\Test', + 'key' => 'siteName', + 'value' => 'Jack', + 'type' => 'string', + 'context' => 'context:male', + ]); + } }