Welcome Guest, Not a member yet? Register   Sign In
settings service: retrieve all properties of a class
#5

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!
Reply


Messages In This Thread
RE: settings service: retrieve all properties of a class - by BhambriRohunu - 11-13-2023, 04:05 AM



Theme © iAndrew 2016 - Forum software by © MyBB