Post by TomSel » Mon Jun 03, 2013 2:44 am

I was just wondering what would be considered the best practice for storing extension specific settings?

At the moment I'm storing it in a table called `extension_name_settings`, but as I develop it further and require more settings, it involves altering the schema and means ensuring backwards compatibility with older versions becomes quite tedious.

Is there a standardised way of doing this? I've searched and couldn't find any mention of how other developers go about doing this.

Thanks,

Tom

Newbie

Posts

Joined
Mon Aug 13, 2012 4:19 am

Post by rph » Mon Jun 03, 2013 4:10 am

Is there a reason you're not using the settings table?

-Ryan


rph
Expert Member

Posts

Joined
Fri Jan 08, 2010 5:05 am
Location - Lincoln, Nebraska

Post by TomSel » Mon Jun 03, 2013 5:35 am

I've searched and read through various documentation, but never seen it mentioned. Could you point me in the direction of somewhere I can read up on it?

Thanks

Newbie

Posts

Joined
Mon Aug 13, 2012 4:19 am

Post by rph » Mon Jun 03, 2013 7:02 am

There is none but here's a little primer.

Setting a value in admin you first need to load the setting model:

Code: Select all

$this->load->model('setting/setting'); 
The four methods are getSetting(), editSetting(), deleteSetting(), and editSettingValue().

They are called the usual way with:

Code: Select all

$this->model_setting_setting->method_name(); 
editSetting()
editSetting(string $group, array $data, [int $store_id = 0])

Used to add or edit values to the settings table. Will delete any matching group values before insert. You'll usually name the group after your extension (as unique as possible) and pass $_POST info for for the data. If no store_id is specified the default 0 will be used.

The $data array is looped through. The key is stored in the database as `key` and value as `value`. The `value` may also be an array, in which case it is serialized. So if you had HTML like this:

Code: Select all

<input type="text" name="my_extension_limit" value="25">

<input type="checkbox" name="my_extension_size[]" value="100" checked="checked" />
<input type="checkbox" name="my_extension_size[]" value="200" checked="checked" />
<input type="checkbox" name="my_extension_size[]" value="300" checked="checked" />
You would pass the posted data:

Code: Select all

if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
    $this->model_setting_setting->editSetting('my_extension', $this->request->post);
} 
You would do other things with the above like validate the input data and user permissions along with setting any success messages and redirects after completion, but this gives you a rough idea of what to do.

editSetting() will then delete any prior settings for the my_extension group out of the table and insert the new values. Assuming there are no changes to the data, the executed SQL would be:

Code: Select all

DELETE FROM setting WHERE store_id = '0' AND `group` = 'my_extension';
INSERT INTO setting SET store_id = '0', `group` = 'my_extension', `key` = 'my_extension_limit', `value` = '25';
INSERT INTO setting SET store_id = '0', `group` = 'my_extension', `key` = 'my_extension_size', `value` = 'a:3:{i:0;i:100;i:1;i:200;i:2;i:300;}', serialized = '1';
getSetting()
getSetting(string $group, [int $store_id = 0])

Returns all the values from $group in an array. Empty array on no matching values. Any serialized values will be unserialized.

Code: Select all

$settings = $this->model_setting_setting->getSetting('my_extension'); 
You can thing grab values with:

Code: Select all

$this->data['my_extension_limit'] = $settings['my_extension_limit'];
Note: Unless you need to grab all your extension settings it's usually easier and less likely to throw an error to get them as needed with $this->config->get('my_extension_key').

deleteSetting()
deleteSetting(string $group, [int $store_id = 0])

Deletes all values from the setting table in the matching group.

Code: Select all

$this->model_setting_setting->deleteSetting('my_extension');
editSettingValue()
editSettingValue([string $group = ''], [string $key = ''], [mixed $value = ''], [int $store_id = 0])

Updates specific setting matching $key. $value can be a string or array.

Code: Select all

$this->model_setting_setting->editSettingValue('my_extension', 'my_extension_limit', $this->request->post['my_extension_limit']); 
Using Settings
Once you have the values in the settings table they'll be automatically loaded. Then it's a matter of using them with the config object.

OpenCart loads all the setting table values into memory for the store through index.php. You can then access/manipulate them with:

Code: Select all

$this->config->get('my_extension_key'); // Returns value or null (be careful, a value can evaluate to false)
$this->config->has('my_extension_key'); // Returns true or false
$this->config->set('my_extension_key'); // Replaces a value in memory           

-Ryan


rph
Expert Member

Posts

Joined
Fri Jan 08, 2010 5:05 am
Location - Lincoln, Nebraska

Post by TomSel » Mon Jun 03, 2013 7:40 am

Wow, that's a great help! Thanks a lot, very much appreciated. 8)

Shame this documentation doesn't already exist though.

Tom

Newbie

Posts

Joined
Mon Aug 13, 2012 4:19 am

Post by Grandstand » Thu Aug 21, 2014 10:24 am

How does "$this->config->get('my_extension_key')" avoid getting a setting from another extension that has the same key name?

New member

Posts

Joined
Sat Aug 27, 2011 8:03 am

Post by Johnathan » Sat Aug 23, 2014 12:42 am

Key names should be prefixed with the group name. That's why settings are prefixed with "config_" for config settings, "featured_" for the featured module, etc.

Yes, this is kind of redundant since the group already exists, but that's how OpenCart was coded. Probably it should be something like this:

$this->config->get($group, $key)

such as $this->config->get('config', 'error_display'). But that would be up to Daniel to change.

Image
Image Image Image Image


User avatar
Global Moderator

Posts

Joined
Fri Dec 18, 2009 3:08 am


Post by Qphoria » Sat Aug 23, 2014 1:33 am

Johnathan wrote:Key names should be prefixed with the group name. That's why settings are prefixed with "config_" for config settings, "featured_" for the featured module, etc.

Yes, this is kind of redundant since the group already exists, but that's how OpenCart was coded. Probably it should be something like this:

$this->config->get($group, $key)

such as $this->config->get('config', 'error_display'). But that would be up to Daniel to change.

yea I always noticed this too.... very redundant but it doesn't hurt anything so no harm no foul

Image
Donate!|OpenCart Basics|GeoZones
Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am
Who is online

Users browsing this forum: No registered users and 7 guests