0

I made a custom module which runs during cron via de cron_hook. On installation of the module a database table is installed:

  $schema['license_expire'] = array(
    'description' => 'The base table for license_expire.',
    'fields' => array(
      'uid' => array(
        'description' => 'The primary identifier for a user.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),
      'warned_user' => array(
        'description' => 'User warning',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),
    ),
    'primary key' => array('uid'),
  );
  return $schema;

When the cron runs content is added to the database. Say uid = 1 and warned_user = 1.

But when I try to fetch this warned_user data in de cron_hook it doesn't return any.

function hook_cron() {
//EXPIRE VARIABLES
$expiresMonth = strtotime('+1 month');
$expiresWeek = strtotime('+1 week');
$expired = strtotime('today');

//ALL USERS EXPIRING WITHIN ONE MONTH
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'commerce_license')
->propertyCondition('status', COMMERCE_LICENSE_ACTIVE)
->propertyCondition('expires', 0, '<>')
->propertyCondition('expires', $expiresMonth, '<');
$results = $query->execute();

//SEND EMAIL TO USERS
if (!empty($results['commerce_license'])) {
    $license_ids = array_keys($results['commerce_license']);

    //all licenses
    foreach($license_ids as $license_id) {

       //get Uid
       $myUid = db_select('commerce_license','cl')
         ->fields('cl', array('uid'))
         ->condition('license_id', $license_id)
         ->execute()->fetchField();

       //for testing it only works for de admin account  
       if($myUid == 1){
           //get user
           $to = user_load($myUid);

           //get expiring timestamp
           $myExpire = db_select('commerce_license','cl')
             ->fields('cl', array('expires'))
             ->condition('license_id', $license_id)
             ->execute()->fetchField();

           $myDate = date("d-m-Y", $myExpire);

           //get times user is noticed
            $myMessages = db_select('license_expire','le')
            ->fields('le', array('warned_user'))
            ->condition('uid', $myUid)
            ->execute()->fetchField();

           //for some reason the $myMessages value is nothing so watchdog is "myMails = = 1"
           watchdog('license expire', 'myMails = '.$myMessages.' = '.$myUid);

           if($myMessages){do something, but it never does because it can't pull $myMessages value from the db, and it sure is there}

The above code does work when I use it in a page.tpl.php file.

I found something about, cron being a Anonymous user and this could be the problem but the solution offered didn't work for me. What dit I forget/overlook/do wrong?

  • Try to use db_select('license_expire')->fields('warned_user')->condition('uid',1,'=')->execute()->fetchAssoc() – jmu Jul 11 '16 at 15:39
  • Hi jmu, thanks for your suggestion. I tried it out but with no succes. Any idea what the problem could be? – Joost Jul 11 '16 at 20:59
  • 1. did you clear cache after you modified? 2. if you use $query = db_select('license_expire')->fields('warned_user')->condition('uid',1,'=')->exe‌​cute()->fetchAssoc();, please add $myMessages = $query['warned_user'] 3. If none of them works, use print_r($query) to see what the $query includes – jmu Jul 11 '16 at 21:23
  • your query doesn't require any access check. You should check in "recent logs". – Ankit Chauhan Jul 12 '16 at 5:36
  • the code jmu gives gives a big error for some reason it is looking for the warned_user table?? And my code doesn't give any recent log message. As said in my original question the query seems to be right it is working in page.tpl.php when I was testing and it does work in another module. I changed my original post added the full module code. This might help.. – Joost Jul 12 '16 at 19:38
0

I found the problem, I was using the same variable names (for example $query) in different custom modules I build. Changing these variable names to unique names made it work perfectly.

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.