<p>You don't need to create Records, Models or interact with the database to store your plugin settings – Craft handles this for you out of the box. Here's how:</p>

<p><strong>In your main plugin file (PluginNamePlugin.php):</strong></p>

<pre><code>protected function defineSettings()
{
    return array(
        'radioGroupName' =&gt; array(AttributeType::String, 'default' =&gt; 'option_1')
    );
}

public function getSettingsHtml()
{
   return craft()-&gt;templates-&gt;render('pluginname/settings', array(
       'settings' =&gt; $this-&gt;getSettings()
   ));
}
</code></pre>

<p>At a glance, your settings template looks fine, and should be placed within the <code>pluginname/templates/</code> directory (filename <code>settings.html</code>).</p>

<p>If you want to retrieve a setting somewhere else within your plugin's main class, you can do it like this:</p>

<pre><code>$settings = $this-&gt;getSettings();
$radioGroupName = $settings-&gt;radioGroupName;
</code></pre>

<p>To read the settings outside the main plugin class, you'll first need to pull the plugin instance, like this:</p>

<pre><code>$plugin = craft()-&gt;plugins-&gt;getPlugin('pluginName');
$settings = $plugin-&gt;getSettings();
...
</code></pre>

<p>If you want to retrieve your setting(s) for use in <em>templates</em>, you'll need to create a template <a href="http://buildwithcraft.com/docs/plugins/variables" rel="noreferrer">Variable</a> class for your plugin in order to expose the data. Here's a quick example for your use case (the following would go into the file <code>pluginname/variables/PluginNameVariable.php</code>:</p>

<pre><code>class PluginNameVariable
{
    public function settings()
    {
        $plugin = craft()-&gt;plugins-&gt;getPlugin('pluginName');
        return $plugin-&gt;getSettings();
    }
}
</code></pre>

<p>...and in your template:</p>

<pre><code>{% set settings = craft.pluginName.settings %}
{{ settings.radioGroupName }}
</code></pre>

<p><em>For more info on plugin settings, see the official docs on the subject:</em> <a href="http://buildwithcraft.com/docs/plugins/plugin-settings" rel="noreferrer">http://buildwithcraft.com/docs/plugins/plugin-settings</a></p>

CC BY-SA 3.0
2015-06-19T09:41:40.153Z
Thanks for your response.  My ultimate goal is to use this information in a template file -- does this mean that I do NOT need to save the information to the database in order to do that?  If so, when does one need to set up models, records and services to interact with the database?
Moshe
CC BY-SA 3.0
2015-06-19T09:43:45.717Z
If you want to retrieve a setting for use in a template, you'll need to create a [Template Variable](http://buildwithcraft.com/docs/plugins/variables) class for your plugin. I can edit my answer to include some info on that.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-06-19T09:54:21.797Z
Your second question is a bit too open-ended to answer here (you might want to create separate thread(s) for any questions you might have regarding plugins and the database). Basically though, if you need to store any data beyond simple settings you'll need to look into Records, Models and Services (and more often than not, Controllers as well). I'd also suggest you take a look at [the official plugin docs](http://buildwithcraft.com/docs/plugins/introduction) if you haven't already – they provide a good overview of how plugins are built/structured.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-06-19T11:35:03.907Z
Thanks for the help.  I got it working :).  I have been checking the plugin docs, but I have been having a hard time understanding what I'm suppose to do from them.  Your answer helped set me in the right direction.
Moshe
CC BY-SA 3.0
2015-06-19T11:37:28.877Z
That's good to hear @Moshe – glad I could help out.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-06-19T09:23:32.150Z
2015-06-19T09:49:39.097Z
2015-06-19T09:49:39.097Z
Mats Mikkel Rummelhoff
<p>I have another plugin development question.</p>

<p>I am now trying to learn how to work with the database.  To that extent, I have the following code:</p>

<p>On the templates/settings.html page I have the following code:</p>

<pre><code>{% import "_includes/forms" as forms %}

{{ forms.radioGroup({
    label: "Radio Group Label"|t,
    instructions: "Radio Group Instructions"|t,
    name: 'radioGroupName',
    options:{ "option_1" : "option_1", "option_2" : "option_2"},
    value: settings.radioGroupName
}) }}
</code></pre>

<p>On the records/PluginNameRecord.php page, I have the following code:</p>

<pre><code>&lt;?php
namespace Craft;

class PluginNameRecord extends BaseRecord
{
    public function getTableName()
    {
        return 'pluginname';
    }


    public function defineAttributes()
    {
        return array(
            'radioGroupName' =&gt; array(AttributeType::String, 'default' =&gt; 'option_1')
        );
    }

}
</code></pre>

<p>And here is the code for the models/PluginNameModel.php file (I don't know if this is relevant or not, but I'm including it just in case):</p>

<pre><code>&lt;?php
namespace Craft;

class PluginNameModel extends BaseModel
{

    public function defineAttributes()
    {
        return array(
            'radioGroupName' =&gt; array(AttributeType::String, 'default' =&gt; 'option_1')
        );
    }
}
</code></pre>

<p>Now, I have tested this and it creates a table and column in mysql.  However, I cannot update the data in the database.  That is to say, if I select option_2 nothing happens in mysql.  The same default value of option_1 still shows up.</p>

<p>Can someone please let me know what I am doing wrong?  </p>

<p>Thanks,</p>

<p>Moshe</p>

<plugin-development><database>
Updating the Database for plugin settings
# Updating the Database for plugin settings

(Asked by: username_0 on 2015-06-18)

I have another plugin development question.
I am now trying to learn how to work with the database.  To that extent, I have the following code:
On the templates/settings.html page I have the following code:
{% import "_includes/forms" as forms %}

{{ forms.radioGroup({
    label: "Radio Group Label"|t,
    instructions: "Radio Group Instructions"|t,
    name: 'radioGroupName',
    options:{ "option_1" : "option_1", "option_2" : "option_2"},
    value: settings.radioGroupName
}) }}

On the records/PluginNameRecord.php page, I have the following code:
 array(AttributeType::String, 'default' => 'option_1')
        );
    }

}

And here is the code for the models/PluginNameModel.php file (I don't know if this is relevant or not, but I'm including it just in case):
 array(AttributeType::String, 'default' => 'option_1')
        );
    }
}

Now, I have tested this and it creates a table and column in mysql.  However, I cannot update the data in the database.  That is to say, if I select option_2 nothing happens in mysql.  The same default value of option_1 still shows up.
Can someone please let me know what I am doing wrong?  
Thanks,
Moshe

**Tags:** plugin-development, database

---

## 1 answers

### Answer by username_1 on 2015-06-19. Score: 5 | Accepted answer

You don't need to create Records, Models or interact with the database to store your plugin settings – Craft handles this for you out of the box. Here's how:
In your main plugin file (PluginNamePlugin.php):
protected function defineSettings()
{
    return array(
        'radioGroupName' => array(AttributeType::String, 'default' => 'option_1')
    );
}

public function getSettingsHtml()
{
   return craft()->templates->render('pluginname/settings', array(
       'settings' => $this->getSettings()
   ));
}

At a glance, your settings template looks fine, and should be placed within the pluginname/templates/ directory (filename settings.html).
If you want to retrieve a setting somewhere else within your plugin's main class, you can do it like this:
$settings = $this->getSettings();
$radioGroupName = $settings->radioGroupName;

To read the settings outside the main plugin class, you'll first need to pull the plugin instance, like this:
$plugin = craft()->plugins->getPlugin('pluginName');
$settings = $plugin->getSettings();
...

If you want to retrieve your setting(s) for use in templates, you'll need to create a template Variable class for your plugin in order to expose the data. Here's a quick example for your use case (the following would go into the file pluginname/variables/PluginNameVariable.php:
class PluginNameVariable
{
    public function settings()
    {
        $plugin = craft()->plugins->getPlugin('pluginName');
        return $plugin->getSettings();
    }
}

...and in your template:
{% set settings = craft.pluginName.settings %}
{{ settings.radioGroupName }}

For more info on plugin settings, see the official docs on the subject: http://buildwithcraft.com/docs/plugins/plugin-settings

> **Comments:**
> **username_0**: Thanks for your response.  My ultimate goal is to use this information in a template file -- does this mean that I do NOT need to save the information to the database in order to do that?  If so, when does one need to set up models, records and services to interact with the database?  
> **username_1**: If you want to retrieve a setting for use in a template, you'll need to create a [Template Variable](http://buildwithcraft.com/docs/plugins/variables) class for your plugin. I can edit my answer to include some info on that.  
> **username_1**: Your second question is a bit too open-ended to answer here (you might want to create separate thread(s) for any questions you might have regarding plugins and the database). Basically though, if you need to store any data beyond simple settings you'll need to look into Records, Models and Services (and more often than not, Controllers as well). I'd also suggest you take a look at [the official plugin docs](http://buildwithcraft.com/docs/plugins/introduction) if you haven't already – they provide a good overview of how plugins are built/structured.  
> **username_0**: Thanks for the help.  I got it working :).  I have been checking the plugin docs, but I have been having a hard time understanding what I'm suppose to do from them.  Your answer helped set me in the right direction.  
> **username_1**: That's good to hear @Moshe – glad I could help out.  

---

