Appearance
<div style="display: none;" hidden="true" aria-hidden="true">Are you an LLM? You can read better optimized documentation at /guides/settings_in_database.md for this page in Markdown format</div>
Settings in database
Create/Move settings.
1. Create a patch in application/controllers/Patch.php
naming: Group names and setting keys must be typed in uppercase and delimited with an underscore.
When you are moving an existing settings in config to database, you should consider using the old keys to migrate the previous values.
To execute the function in terminal, use php cli.php patch/productSettings
php
protected function productSettings()
{
$settingsGroupName = 'PRODUCT';
$settings = [
[
"key" => "DISCOUNT_IS_ENABLED",
"old_key" => "productDiscounts"
],
[
"key" => "BADGE_TEXT",
"old_key" => "productBadgeDefaultText"
],
];
foreach ($settings as $setting) {
$this->registry->set_regval($setting["key"], $settingsGroupName, '', config_item($setting["old_key"]));
}
echo $settingsGroup . " settings added to registry \n";
}.saving array element from existing array
php
$this->registry->setArray("% REGISTRY_KEY %", $settingsGroup, '', config_item('arrayName')["arrayElementName"]);saving an array value of a nested array
php
$this->registry->setArray("% REGISTRY_KEY %", $settingsGroup, '', config_item('agoraSettings')['arrayElement']['arrayChildElement']);1.1 Create new setting
When creating a setting use default_value instead an old_key
php
protected function productSettings()
{
$settingsGroupName = 'PRODUCT';
$settings = [
[
"key" => "DISCOUNT_IS_ENABLED",
"default_value" => "productDiscounts"
],
];
foreach ($settings as $setting) {
$this->registry->set_regval($setting["key"], $settingsGroupName, '', config_item($setting["default_value"]));
}
echo $settingsGroup . " settings added to registry \n";
}.2. Register values in ecommercen/settings/controllers/Adv_settings.php
naming: lowercase group name + value connected with an underscore.
Create or use an existing function that corresponds to your settings group.
Each function usually consist of render, validate and save blocks.
render
in render block we register variables for the frontend.
'registry_group_name_registry_value' => $this->registry->get_regval(REGISTRY_VALUE, REGISTRY_GROUP_NAME)
validate
in validation block we validation incoming post request values, usually only trim.
$this->form_validation->set_rules('registry_group_name_registry_value', '', 'trim')
save
in saving block we save to database.
$this->registry->set_regval('REGISTRY_VALUE', 'REGISTRY_GROUP_NAME', '', $this->input->post('registry_group_name_registry_value', true));
when saving a boolean value from a post request consider adding the type ( int ) type
$this->registry->set_regval('REGISTRY_VALUE', 'REGISTRY_GROUP_NAME', '', (int) $this->input->post('registry_group_name_registry_value', true));
php
public function third_party_providers()
{
if ($this->input->post('submit')) {
$this->thirdPartyProvidersValidation();
if ($this->form_validation->run()) {
{% Save %}
$this->registry->set_regval('DISCOUNT_IS_ENABLED', 'PRODUCT', '', (int) $this->input->post('product_discount_is_enabled', true));
$this->registry->set_regval('BADGE_TEXT', 'PRODUCT', '', $this->input->post('product_badge_text', true));
redirect('settings/third_party_providers');
}
}
$this->render = array_merge($this->render, [
{% Render %}
'product_discount_is_enabled' => $this->registry->get_regval('DISCOUNT_IS_ENABLED', 'PRODUCT'),
'product_badge_text' => $this->registry->get_regval('BADGE_TEXT', 'PRODUCT'),
'view_content' => "{$this->classView}/third_party_providers"
]);
$this->defaultRender();
$this->load->view($this->template_view_admin, $this->render);
}
protected function thirdPartyProvidersValidation()
{
{% Validate %}
$this->form_validation
->set_rules('product_discount_is_enabled', '', 'trim')
->set_rules('product_badge_text', '', 'trim');
}3. UI form for settings in application/views/admin/settings
Create or use an existing form that corresponds to your settings group.
php
<?= form_open(); ?>
// Boolean
<input type="checkbox" name="product_discount_is_enabled" value="1" <?= set_checkbox('product_discount_is_enabled', '1', (bool)$product_discount_is_enabled); ?>>
// Text
<input class="form-control" type="text" name="product_badge_text" value="<?= set_value('product_badge_text', $product_badge_text);?>">
<button type="submit" name="submit" value="true"> save all </button>
<?= form_close(); ?>after creating a form or the fields, it is good to perform a save check, and see if values are saved.
4. Set settings in application/views/admin/settings
naming: camelCase group name + value
we register our settings group in front controller.
php
$productSettings = $this->registry->getGroupAsArray('PRODUCT');
$this->settings = [
'productDiscountIsEnabled' => (bool) $criteoSettings['DISCOUNT_IS_ENABLED'] ?? false,
'productBadgeText' => $criteoSettings['BADGE_TEXT'] ?? '',
];5. Replace/Insert settings
Replace or insert settings depending on file scopes.
php
If view file: $settings['productBadgeText']
If controller: $this->settings['productBadgeText']
If admin view : $this->registry->get_regval('BADGE_TEXT', 'PRODUCT')
If admin controller : $this->registry->get_regval('BADGE_TEXT', 'PRODUCT')
If adv front controller : $this->registry->get_regval('BADGE_TEXT', 'PRODUCT')
If model : $this->registry->get_regval('BADGE_TEXT', 'PRODUCT')
If helper:
$ci =& get_instance();
$ci->registry->get_regval('BADGE_TEXT', 'PRODUCT');