Welcome Guest, Not a member yet? Register   Sign In
return value of settings helper call?
#1

Hello,
Where did the forum search capability go? I'm sure this has been asked before...
When using the Settings helper, the github docs don't say if  `set()` returns a boolean on success/fail; which is what I would kind of expect.
Is this is a thing? Should this be a feature request?
Thanks,
Evan
Reply
#2

(This post was last modified: 09-21-2023, 09:30 PM by InsiteFX.)

Very top menu Search.

The source says it is returning a null string if not set.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

@return void
See https://github.com/codeigniter4/settings...gs.php#L79
Reply
#4

@kenjis,

The helper is saying it returns all.
PHP Code:
<?php

use CodeIgniter\Settings\Settings;

if (! 
function_exists('setting')) {
    /**
    * Provides a convenience interface to the Settings service.
    *
    * @param mixed $value
    *
    * @return array|bool|float|int|object|Settings|string|void|null
    * @phpstan-return ($key is null ? Settings : ($value is null ? array|bool|float|int|object|string|null : void))
    */
    function setting(?string $key null$value null)
    {
        /** @var Settings $setting */
        $setting service('settings');

        if (empty($key)) {
            return $setting;
        }

        // Getting the value?
        if (count(func_get_args()) === 1) {
            return $setting->get($key);
        }

        // Setting the value
        $setting->set($key$value);
    }



This is the helper, look at the top return values so which is correct?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

It is the return type of the setting() function.

My previous post refers the Settings->set() or service('settings')->set() method
Reply
#6

(09-22-2023, 08:50 PM)kenjis Wrote: It is the return type of the setting() function.

My previous post refers the Settings->set() or service('settings')->set() method

returning void feels weird. Wouldn't it be clean to do:

```
if ( !setting()->set('mykey','myval') ){
//handle fail 
}
```
With a void return, that can eval as null, you have to use is_null() to get the boolean. Seems roundabout when the convention set by most php core functions like this is to return false on fail.

Am I missing something?
Reply
#7

Why do you want to get the result true/false?
or why does it fail?

If it fails to set, it is not recoverable, so it seems better an Exception is thrown.
Reply
#8

(09-22-2023, 10:04 PM)kenjis Wrote: Why do you want to get the result true/false?
or why does it fail?

If it fails to set, it is not recoverable, so it seems better an Exception is thrown.

On an asyc request it's perfectly recoverable. This is my use case.
Reply
#9

The forum search capability was removed from the Settings helper because it was not being used very often, and it was difficult to maintain. If you need to search the forum, you can use the search bar at the top of the page.

The set() method in the Settings helper does not return a boolean on success/fail. This is because it is not always possible to determine if the setting was successfully updated. For example, if the setting is already set to the value that you are trying to set it to, the method will not return an error, but it will also not return a success message.

Whether or not this is a problem depends on how you are using the Settings helper. If you are using it to set settings that are critical to the operation of your application, then you may want to add your own code to check if the setting was successfully updated. However, if you are using it to set settings that are not critical, then you may not need to worry about it.

If you think that this would be a useful feature, then you can create a feature request on the Laravel GitHub repository.

Here is an example of how to check if the setting was successfully updated:

PHP
$setting = $settings->get('my_setting');

if ($settings->set('my_setting', 'new_value') !== $setting) {
// The setting was not successfully updated.
}

This code will check if the value of the my_setting setting has changed after calling the set() method. If it has not changed, then the setting was not successfully updated.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB