File: /var/www/heifetz/heifetz-app/crons/NotificationRotation.php
<?php
declare(strict_types=1);
namespace Crons;
use Models\Notification;
class NotificationRotation extends AbstractCron
{
protected string $title = 'Удаляем старые оповещения';
public function execute(): void
{
$checkDate = date('Y-m-d', strtotime('-1 week'));
$totalCount = Notification::model()->where('created_at <= ?', $checkDate)->totalCount();
if (empty($totalCount)) {
$this->log('Нечего переносить');
return;
}
$this->log('Удаляем оповещения старее ' . date('d.m.Y', strtotime($checkDate)));
$deletedCount = Notification::$db->query('DELETE FROM ' . Notification::$tableName . " WHERE DATE(created_at) < '" . $checkDate . "';");
$this->log('Удалено: ' . $deletedCount . ' записей');
}
}