<p>You could create a <a href="http://buildwithcraft.com/docs/plugins/records" rel="nofollow" title="Record">Record</a> which will give you a custom table to store your twitter related settings and update them as needed but I think that your use case is still simple enough that you could leave them in the <code>settings array</code> and then use the <code>Plugins Service</code> to update those settings.</p>

<p>This is how I would do it if I chose to use <code>defineSettings()</code></p>

<hr>

<pre class="lang-php prettyprint-override"><code>// MyPlugin.php
public function defineSettings()
{
   return array(
        // ...
        'twitterApiKey' =&gt; array(AttributeType::String, 'default' =&gt; '5omeRand0mstriNg'),
        'twitterUsername' =&gt; array(AttributeType::String, 'default' =&gt; 'someone'),
    );
}
</code></pre>

<pre class="lang-php prettyprint-override"><code>// MyService.php
public function saveTwitterSettings(array $settings=array())
{
    $plugin = craft()-&gt;plugins-&gt;getPlugin('myplugin');
    $savedSettings = $plugin-&gt;getSettings()-&gt;getAttributes();
    $newSettingsRow = array_merge($savedSettings, $settings);

    return craft()-&gt;plugins-&gt;savePluginSettings($plugin, $newSettingsRow);
}
</code></pre>

<hr>

<p>Feel free to follow up with questions/comments if you have any.</p>

<p><a href="http://twitter.com/selvinortiz" rel="nofollow" title="Selvin Ortiz">@selvinortiz</a></p>

CC BY-SA 3.0
2014-06-20T13:19:50.230Z
Thanks Selvin, Records was the piece of the puzzle I was missing. I do also want to keep a log of each run of the plugin, so I think in this instance a Record may make more sense. Your code will come in handy next time for instances where a Record is a little too much.
Steve Holland
CC BY-SA 3.0
2014-06-20T13:33:28.243Z
Thanks for adding the link to the docs for the record @Steve and yes, in that case you'll definitely need one. Craft makes it a joy to interact with the database via records and the query builder: )
Selvin Ortiz
CC BY-SA 3.0
2014-06-20T12:36:01.493Z
2014-06-23T17:04:52.790Z
2014-06-23T17:04:52.790Z
Selvin Ortiz
<p>For instance I'm writing a plugin which is going to be periodically pulling stuff from Twitter, so I'd like to keep a hold of when the plugin was last run and maybe some Ids relevant to Twitter that sort of thing. </p>

<p>I know there is the settings array which you use for user defined values, should I also use it for other stuff that the user can't change, or is there a more sensible place to stick those?  </p>

<plugin-development>
When developing plugins where should you store values that might not strictly be settings?
# When developing plugins where should you store values that might not strictly be settings?

(Asked by: username_0 on 2014-06-20)

For instance I'm writing a plugin which is going to be periodically pulling stuff from Twitter, so I'd like to keep a hold of when the plugin was last run and maybe some Ids relevant to Twitter that sort of thing.

I know there is the settings array which you use for user defined values, should I also use it for other stuff that the user can't change, or is there a more sensible place to stick those?

**Tags:** plugin-development

---

## 1 answers

### Answer by username_1 on 2014-06-20. Score: 7 | Accepted answer

You could create a Record which will give you a custom table to store your twitter related settings and update them as needed but I think that your use case is still simple enough that you could leave them in the settings array and then use the Plugins Service to update those settings.
This is how I would do it if I chose to use defineSettings()

// MyPlugin.php
public function defineSettings()
{
   return array(
        // ...
        'twitterApiKey' => array(AttributeType::String, 'default' => '5omeRand0mstriNg'),
        'twitterUsername' => array(AttributeType::String, 'default' => 'someone'),
    );
}

// MyService.php
public function saveTwitterSettings(array $settings=array())
{
    $plugin = craft()->plugins->getPlugin('myplugin');
    $savedSettings = $plugin->getSettings()->getAttributes();
    $newSettingsRow = array_merge($savedSettings, $settings);

    return craft()->plugins->savePluginSettings($plugin, $newSettingsRow);
}


Feel free to follow up with questions/comments if you have any.
@selvinortiz

> **Comments:**
> **username_0**: Thanks Selvin, Records was the piece of the puzzle I was missing. I do also want to keep a log of each run of the plugin, so I think in this instance a Record may make more sense. Your code will come in handy next time for instances where a Record is a little too much.  
> **username_1**: Thanks for adding the link to the docs for the record @Steve and yes, in that case you'll definitely need one. Craft makes it a joy to interact with the database via records and the query builder: )  

---

