I am using the following code, but it doesn't work.

  function mymodule_cron() {
    // ...


    // ...

    function _update_user_categories($uid) {

      $articleCategories = array();
      $user = user_load($uid, TRUE);

    // Fill array with user access categories
      foreach ($user->field_article_category['und'] as $index => $tid) {
        $articleCategories[] = $tid['tid'];

    // Generate SQL statement for Multi record insert
      $values = array();
      $accessValues = array();
      foreach ($articleCategories as $index => $value) {
        $accessValues[] = $value;
        $terms = taxonomy_get_tree(2, $value);
    // Loop for all children to add them in the $accessValues;
        foreach ($terms as $term) {
          $accessValues [] = $term->tid;
      $categories = array();        
      foreach (array_unique($accessValues) as $index => $value) {
        if ($value) {
          $values[$value] = "($uid,$value,'taxonomy')";
          if (!in_array($value, $categories)) {
            $categories [] = array('tid' => $value);

      $insValues = implode(', ', $values);
      if (!empty($insValues)) {
        $sqlDel = 'DELETE FROM {workbench_access_user} WHERE uid = ' . $uid;
        $sqlIns = 'INSERT INTO {workbench_access_user} (uid, access_id, access_scheme)         VALUES ' . $insValues;
      $edit = array(        
        'field_article_category' => array(
          'und' => $categories,
      user_save($user, $edit);

No data is returned by taxonomy_get_tree(). user_save() is not working, but it works when called outsite hook_cron().

What am I doing wrong?

taxonomy_get_tree($vid, $tid); returns empty array if i called my function from hook_cron

but if i called it from any others place it works.

  • Thanks for adding the code - and you're absolutely, 100% sure that $uid is reliably the same both in and out of the context of your cron hook? Double and triple check that by calling _update_user_categories(1) manually (with an appropriate hard-coded user ID), then change $uid to the same integer in your cron hook. That's the best way to get a reliable test, and if fails you know for sure the problem is somewhere in _update_user_categories(). Currently it's hard to tell because we can't see how $uid is being initialised – Clive Sep 26 '13 at 14:02
  • This function doesn't have this case _update_user_categories(1) because actually there are just two places to call this function and no one has user one. It is running from another place from hook_form_alter but from hook_cron it fails . – Mohammad Jolani Sep 26 '13 at 14:06
  • 1
    You're calling the function in two different contexts - to debug, you need to hard-code the $uid parameter for both contexts and compare the results. We're talking really basic debugging here, you can ignore it if you want but it's not going to get your problem solved – Clive Sep 26 '13 at 14:11
  • I have exactly the same issue. print_r(taxonomy_term_load(150)) works when called normally, but is empty when called via hook_cron – Felix Eve Dec 16 '13 at 11:55
  • May you show how you set $uid in mymodule_cron()? Without that is impossible to answer. – kiamlaluno Jul 13 '16 at 11:49

Your Answer

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

Browse other questions tagged or ask your own question.