CodeIgniter Forums
settings service: retrieve all properties of a class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: settings service: retrieve all properties of a class (/showthread.php?tid=88644)



settings service: retrieve all properties of a class - evansharp - 10-10-2023

The docs don't mention (and I've been unable to hit upon) a way to retrieve all of the stored values for a whole class of properties. Is this a feature? Can I submit a request/ PR?

The use-case is pretty standard I would think: my app has a configuration page. I have created a class in App/Config to hold the defaults and want to store user changes with the convenient service. When I load the configuration page, I'd like to be able to call the service once and get all non-default saved settings in the class rather than querying the database and having to name each property specifically. Bonus points if a wildcard `get()` intelligently grabs the class defaults and overrides them with a db-saved value if extant.

Something like:
Code:
$siteName = service('settings')->get('App.[b]*[/b]');

It seems natural that this would return an associative array of the properties as k=>v.

Thoughts?


RE: settings service: retrieve all properties of a class - sammyskills - 10-11-2023

Hi @evansharp ,

Not sure I understand what you mean but, can't this be achieved using config() ? Like:

PHP Code:
config(App::class);  



RE: settings service: retrieve all properties of a class - evansharp - 11-03-2023

(10-11-2023, 11:57 PM)sammyskills Wrote: Hi @evansharp ,

Not sure I understand what you mean but, can't this be achieved using config() ? Like:

PHP Code:
config(App::class);  

Is `config()` aware of the settings service? I thought it just grabbed the config file properties; which could be overridden by an unknown number of db-saved user settings.


RE: settings service: retrieve all properties of a class - kenjis - 11-04-2023

(11-03-2023, 11:24 PM)evansharp Wrote: Is `config()` aware of the settings service?

Of course not.


RE: settings service: retrieve all properties of a class - BhambriRohunu - 11-13-2023

You are correct that the CodeIgniter docs do not mention a way to retrieve all of the stored values for a whole class of properties. This is not a current feature of CodeIgniter, but it would be a useful one, and I encourage you to submit a request or PR for it.

Your use case is very common, and it would be great to have a way to implement it in CodeIgniter without having to query the database and name each property specifically.

Your suggestion of a wildcard get() function that intelligently grabs the class defaults and overrides them with db-saved values if extant is a good one. This would allow developers to easily get all of the non-default configuration settings for a class with a single call to the get() function.

Here is an example of how a wildcard get() function could be implemented:

PHP
public function get($key, $default = null)
{
// Get the class defaults.
$defaults = $this->config->get($key);

// Get the database-saved values.
$savedValues = $this->db->get($key);

// Override the defaults with the database-saved values, if extant.
$values = array_merge($defaults, $savedValues);

// Return the values.
return $values;
}

This function would take a class name as the first parameter and a default value as the second parameter. It would then get the class defaults and the database-saved values for the class. The database-saved values would override the defaults, and the function would return an associative array of all of the non-default configuration settings for the class.

I hope this helps!