Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Is there a good (recommended) way to store custom values in Drupal 7? I've looked into variable_set / variable_get, but then read a comment suggesting NOT to use those for the purpose that I'm needing (unless I completely misunderstood). Here's the comment that I'm referring to:

http://api.drupal.org/api/drupal/includes!bootstrap.inc/function/variable_set/7

"All data created by variable_set() is loaded from the database at bootstrap. Calling variable_get() doesn't perform the database request to get you the data, it simply declares the $conf global and returns the variable you've asked for. Therefore, don't carelessly use variable_set() to store lots of data that you will only use on individual pages or in custom methods. Use your own little table and storage functions for that."

Basically, I'd like a place to store a couple of values, so that I can avoid having to hard code those values within my custom module code. For instance, I'd like to store a current Reporting Year (i.e. 2011), and a current Data Entry Year (i.e. 2012). Is this just something that I'd need to create my own table for?

share|improve this question
add comment

3 Answers 3

up vote 1 down vote accepted

No, there's no need to create your own table for that. Your use case is a perfect example of what should be stored in the variables table (in my opinion).

Since the data isn't particularly volatile, and you only have a couple of values, I can't see any benefit to adding a custom table for it.

If you start finding yourself storing a lot of data in variables, for example a single different variable for each node of a certain content type, that's when you need to start looking at a custom table.

share|improve this answer
    
Where would I place the actual variable_set($breakfast, 'donuts')? Is it then accessible anywhere that I call variable_get($breakfast)? –  cmmsites Jul 13 '12 at 12:18
    
You can either define it in $settings.php: $conf['your_variable'] = 'value'; or build a settings form for it, see system_settings_form(). –  Berdir Jul 13 '12 at 13:47
add comment

If you are just using these to to replace constants in your code, then variable_set / variable_get is the appropriate thing to use.

It is not the best approach to persist data between pages (eg, emulate a cookie) or to avoid having to make a custom database table.

share|improve this answer
4  
Note that "not the best approach" actually means "completely wrong approach" if variable_set() is going to be called on public pages/all pages. I had yet another case of a hardcoded variable_set() call in template.php (outside of a function of course. Took me a moment to find it, that's even more crazy than doing it in a .module file) today on a site that I was tasked to do a performance analysis, that's a nice way to completely kill the performance of your site. –  Berdir Jul 9 '12 at 19:05
    
@Berdir: what would you suggest? –  cmmsites Jul 13 '12 at 12:13
    
@cmmsites: Well, anything else, put it into session, a custom table, cache, get arguments, whatever you want. –  Berdir Jul 13 '12 at 13:46
add comment

From what you've said here,

$foo=variable_get('YOURMODULE_current_reporting_year', '2011');

or

$bar=variable_get('YOURMODULE_current_data_year', '2012');

isn't going to really hurt anything.

You could even set up a module admin settings page to set these if/when your years change. Now, if your module was going to variable_set() these all over the place, that would be another story.

Of course, if you are super concerned with the DB, you could also

define('YOURMODULE_CURRENT_REPORTING_YEAR', 2011);
define('YOURMODULE_CURRENT_DATA_YEAR', 2012);

and then this would work:

$foo=YOURMODULE_CURRENT_REPORTING_YEAR;
$bar=YOURMODULE_CURRENT_DATA_YEAR;

but you would have to manually change those defines when necessary.

share|improve this answer
    
Similarly to my response to Clive above, where would I place the actual variable_set()? Same question applies to the define()? I just want to ensure that whatever I approach I take I'm able to access those Report Year and/or Current Data Year values from anywhere. –  cmmsites Jul 13 '12 at 12:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.