0
votes

I need a simple script to force change editedon field of every resource in MODX Revo. This is what I have so far:

$resources = $modx->getCollection('modResource',array('parent' => 0));
$mydate = date(%c);
foreach ($resources as $res) {
    $res->set('editedon', $mydate);
    $res->save();
}

Just to test the script I assigned it to OnBeforeCacheUpdate event, so every time the cache is cleared I could check results. The problem is, that the script hangs itself on Console running... Like just "Hang in 'ere kid..."

What's going on? Thank you.

UPD: When the script is called directly in browser I get this error:

Fatal error: Call to a member function getCollection() on a non-object in /path/script.php

However, some resources have their editedon field succesfully, but not all. Why is that?

UPD2: Ok, probably the script is not going to work on its own, because there are no MODX specific classes and all that inside script, but it seems to be working after I clear cache, because it is assigned to OnBeforeCacheUpdate event. Also I removed array('parent' => 0) part and it now works for every resource (not just ones without children). But how do I run it every 24 hour? Cron is not an option as the script doesn't work on its own.

1
Ok, got it working by using plugin CronManager for MODX.nrvnm

1 Answers

0
votes

Still talking to myself... this is the code that worked:

$resources = $modx->getCollection('modResource');
$mydate = date(%c);
foreach ($resources as $res) {
    $res->set('editedon', $mydate);
    $res->save();
}

Run it via CronManager extra for MODX and there you go. If anyone ever needs it.