SOLVED: Scheduled task php plugin

dimoss

Well-Known Member
Hi,

I have a scheduled task using the following script to update the 'status' of each record in the DB table 'fab_tournaments' when today date is greater that 'tour_end' (datetime):

PHP:
$enddate = strtotime("{fab_tournaments___tour_end}");
$nowdate = strtotime("today");
$non_active = '0';
$active = '1';
$db = JFactory::getDbo();
$db->getQuery(true);
$query = $db->getQuery(true);
foreach ($data as $group) {
   foreach ($group as $row) {
            $query->clear()
            ->update('fab_tournaments')
            ->where('id = ' . (int)$row->id);
if ($nowdate > $enddate)
{
$query->set('status = ' . $db->quote($non_active));
}
else
{
$query->set('status = ' . $db->quote($active));
}
$db->setQuery($query);
$db->execute();
}
}

Running the scheduled task from the backend doesnt update the records as it should be. It always return "0 records updated" which is wrong as there records where the condition is met.

On #__fabrik_log I get:
"8,Undefined property: stdClass::$id,/xxx/xxx/xxxx/plugins/fabrik_cron/php/php.php(75) : eval()'d code,12"

Any help is appreciated.
 
When I change $row->id to $row->fab_tournaments___id I get the follwoing in #__fabrik_log
"8192,array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead,/xxx/xxx/xxx/components/com_fabrik/models/list.php,4494"
 
  • Deprecated messages don't matter, it's only some notice (J!3/F3 is going to end anyway)
  • You don't have $data in a php scheduled task if you don't set it. You can fetch the data yourself from the database or use the listModel, see e.g. the examples in plugins\fabrik_cron\php\scripts
  • Maybe you have to use _raw in the placeholders
Use e.g. var_dump(...);exit;
and "Run" the task manually to see what you get.
 
Hi again,

i changed the PHP code to:


PHP:
$app = JFactory::getApplication();
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$queryString = "SELECT id, tour_end FROM fab_tournaments WHERE status = 1";
$db->setQuery($queryString);
$rows = $db->loadAssocList();

foreach ($rows as $row) {
    $id = $row['id'];
    $enddate = (new DateTime($row['tour_end']))->format('Y-m-d');
    $nowdate = (new DateTime())->format('Y-m-d');
    $query->clear();
    $query->update($db->quoteName('fab_tournaments'))
          ->set($db->quoteName('status') . ' = 0')
          ->where($db->quoteName('id') . ' = ' . (int)$id)
          ->where($db->quoteName('tour_end') . ' < ' . $db->quote($nowdate));
    $db->setQuery($query);
    $db->execute();
}

...and seems is working fine.
However when I run the scheduled taks manually on backend i get the message "0 records updated" while it actually worked and the affected record changed in the DB table.
Am I missing something?
Thanks,
 
You are correct: $data is loaded if a list is selected (so your old code should do also if...whatever ;))

Records updated:
Fabrik doesn't know what you are doing inside php. You must set $processed explicitly
$processed = your-number-of-affeccted-rows;
 
You are correct: $data is loaded if a list is selected (so your old code should do also if...whatever ;))

Records updated:
Fabrik doesn't know what you are doing inside php. You must set $processed explicitly
$processed = your-number-of-affeccted-rows;
Thanks for the advise @troester
 
The code is updated to update all records which fulfill the condition:

PHP:
$app = JFactory::getApplication();
$db = JFactory::getDBO();
$queryString = "SELECT id, tour_end FROM fab_tournaments WHERE status = 1";
$db->setQuery($queryString);
$rows = $db->loadAssocList();

foreach ($rows as $row) {
    $id = $row['id'];
    $enddate = (new DateTime($row['tour_end']))->format('Y-m-d');
    $nowdate = (new DateTime())->format('Y-m-d');

    // Create a new query object for each iteration
    $query = $db->getQuery(true);
    $query->update($db->quoteName('fab_tournaments'))
          ->set($db->quoteName('status') . ' = 0')
          ->where($db->quoteName('id') . ' = ' . (int)$id)
          ->where($db->quoteName('tour_end') . ' < ' . $db->quote($nowdate));

    $db->setQuery($query);
    $db->execute();
}

Just in case someone else wants to do something similar.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top