I want when cron runs to delete the oldest values of timestamp and in same time create the new one. I have this code for cron. Here select all the values from watchdog:
function error_cron() {
// Begin building the query.
$query = db_select('watchdog', 'th')
->extend('PagerDefault')
->orderBy('wid')
->fields('th', array('variables', 'type', 'severity',
'message', 'wid', 'timestamp'))
->limit(2000);
// Fetch the result set.
$result = $query -> execute();
// Loop through each item and add to $row.
foreach ($result as $row) {
error_table($row);
}
In the next code i create the distinct values of the watchdog in my table:
function error_table($row) {
$variables = $row -> variables;
$timestamp = $row -> timestamp;
$wid = $row -> wid;
$r = db_select('error', 'b')
-> fields('b')
-> condition('variables', $variables, '=')
-> condition('timestamp', $timestamp, '<=')
-> execute();
if($r -> rowCount() == 0) {
$query = db_insert('error')
->fields(array(
'timestamp' => $timestamp,
'wid' => $wid,
'variables' => $variables,
))
->execute();
}
}
Ok with all this code the table has been created and it has the distinct values but when cron reruns it shows the same values.. I want when there is a row with same 'variables' to delete the row with the oldest timestamp and create the new one with the newest timestamp.
I solve my problem with this lines of code:
$giannis = db_delete('error')
-> condition('variables', $variables, '=')
-> condition('timestamp', $timestamp, '<')
->execute();