Hello there MODX'ers,
I've got a script that reads data from an API and creates resources from it.
I am setting a date variable from the API and writing it to the publishedon date. However, when I create the resource and save it the first time, it does not respect the value, instead it defaults to 12/15/1969. When I update the resource with the same script and the same variable it updated the publishedon date fine.
Is there some trick for setting the publishedon date on resource creation? Here's a simplified version of my import script:
function importJobs($jobCategory, $modxObject){
$openJobs = apiGetJobs($jobCategory);
foreach($openJobs as $api_value){ // start import
//set variables from api data
$jobId = $api_value["Id"];
$jobPositionTitle = $api_value["PositionTitle"];
$jobCreated = strtotime($api_value["Created"]);
$jobUpdated = $api_value["Updated"];
$document = $modxObject->getObject('modResource',array('longtitle' => $jobId)); // look for existing document
if(!empty($document)){
$modxObject->log(modX::LOG_LEVEL_ERROR,'document exists for job id: '.$jobId.' --- $jobUpdated: '.$jobUpdated);
$document->set('pagetitle', $jobPositionTitle);
$document->set('content', $jobDescription);
$document->set('publishedon', $jobUpdated);
if ($document->save() === false){// Save the document
$modxObject->log(modX::LOG_LEVEL_ERROR,'An error occurred while saving your Resource: '.$jobId);
return false;
}
}else{
$modxObject->log(modX::LOG_LEVEL_ERROR,'document does not exist yet for job id: '.$jobId.' --- $jobUpdated: '.$jobUpdated);
$newDocument = $modxObject->newObject('modResource');
$newDocument->set('pagetitle', $jobPositionTitle);
$newDocument->set('content', $jobDescription);
$newDocument->set('publishedon', $jobUpdated);
$modxObject->log(modX::LOG_LEVEL_ERROR,'page items set');
if ($newDocument->save() === false){ // Save the document
$modxObject->log(modX::LOG_LEVEL_ERROR,'An error occurred while saving your Resource!');
return false;
}else{
$modxObject->log(modX::LOG_LEVEL_ERROR,'New Document saved: '.$jobId);
}
} // close if empty document
} // close $openJobs loop
} // close importJobs()
On lines 15 & 25 I'm logging the $jobUpdated variable before I use it and it logs the same correct date from the variable, but it does not save on resource creation.
I also tried formatting it the same way as the $jobCreated variable, but then it doesn't seem to save at all. I don't think it's a formatting issue anyways since it saves when updating the resource.
I'm running MODX Revo 2.3.5 on MODX Cloud.
If anyone can help, it's much appreciated.
Thanks,
Roger