0

Is variable_set just creating a session variable or can I retrieve this variable from a cron job?

I can load the page over and over, but I always get this output. It seems as though the data is not pulled from the variables table, but set only manually each page load.

$default_vals = get_defaults_variable_get();
clean_dump($default_vals);

save_defaults_variable_set();

$default_vals = get_defaults_variable_get();
clean_dump($default_vals);
clean_dump($_GET);


function get_defaults_variable_get(){
    $content[sort_direction] = variable_get('inventory_sort_direction');
    $content[threshold] = variable_get('inventory_threshold');
    $content[hide_zero_inventory] = variable_get('inventory_hide_zero_inventory');
    $content[email] = variable_get('inventory_email');
    return $content;
}


function save_defaults_variable_set(){
    //echo 'save defaults';
    variable_set('inventory_sort_direction', $_GET[sort_direction]);
    variable_set('inventory_threshold', $_GET[threshold]);
    variable_set('inventory_hide_zero_inventory', $_GET[hide_zero_inventory]);
    variable_set('inventory_email', $_GET[email]);

}

output

array
  'sort_direction' => null
  'threshold' => null
  'hide_zero_inventory' => null
  'email' => null
array
  'sort_direction' => string 'DESC' (length=4)
  'threshold' => string '5' (length=1)
  'hide_zero_inventory' => string 'true' (length=4)
  'email' => string 'e@sltwtr.com,bsaltzman@gmail.com' (length=39)
array
  'threshold' => string '5' (length=1)
  'sort_direction' => string 'DESC' (length=4)
  'hide_zero_inventory' => string 'true' (length=4)
  'email' => string 'e@sltwtr.com,bsaltzman@gmail.com' (length=39)
  'save_to_email' => string 'true' (length=4)
  'q' => string '' (length=0)
  • Presumably the $_GET vars aren't what you expect (you should quote your strings by the way, PHP will assume an undefined constant is a string but you'll only get into trouble doing things that way). Which hook are you running this code in? – Clive Sep 23 '14 at 23:24
  • I am running it in a module function. The get vars are as expected. I am trying to set them permanently with variable_set. As you can see, on each new page load the variables from variable_get are null. – Bryan Sep 23 '14 at 23:27
  • Check your settings.php to make sure there aren't any overrides for your vars on $conf, and flush any non-db caches (memcache, redis, etc) to be doubly sure. Other than that, check that other custom or contrib modules aren't causing it by disabling them temporarily. variable_set and variable_get are fundamental functions to Drupal, if they're not working something is up with your installation. And of course make sure you test the variable_set with literal strings to rule problems with $_GET – Clive Sep 23 '14 at 23:30
1

Straight from the docs:

Sets a persistent variable.

Yes, it will be available across different requests.

  • Thanks! Will it be stored in the database and available to a cron job days later? – Bryan Sep 23 '14 at 23:17
  • Yep, it goes straight in the variable table – Clive Sep 23 '14 at 23:18
1

It will be saved in the variables table and will stay there until it's either deleted or overwritten.

  • That's what I thought, but can you see the problem with the following code. I will update the question now. – Bryan Sep 23 '14 at 23:20
  • In addition, I'd mention that if your variable is declared in a hook_install(), then it will be cleared out when the module is uninstalled. So, this is one of many ways a variable can be deleted. – sheldonkreger Sep 24 '14 at 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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