freedomsex / auto-clean-entity
2.1.10
2022-11-01 23:29 UTC
Requires
- php: ~7.4.0
- freedomsex/base-utilites: ^0.1.1
README
为特定的 Doctrine 实体创建清理器
实体必须包含 id
主字段和一些 date_time
字段
<?php
namespace App\Services\Cleaner;
use FreedomSex\Services\AbstractCleaner;
use App\Repository\MessageRepository;
class MessageCleaner extends AbstractCleaner
{
public $autocleanEnable = true;
public $cleanDaysPeriod = 7;
public $cleanDateField = 'date';
// OR/AND Add batch cleaner params
public function __construct(
DialogRepository $repository
) {
$this->repository = $repository;
}
}
一些设置
public $autocleanEnable = false; // default - disabled
public $autocleanEvery = 10; // conditionally, each (divisible) - 10 - every tenth
public $cleanDaysPeriod = 15; // more than a few days old
public $cleanItemsCount = 100; // select no more items
public $cleanDateField = 'updated'; // `db` datetime field
使用自动清理
// ...
public function __construct(
MessageCleaner $cleaner
) {
$this->cleaner = $cleaner;
}
public function someFunction()
{
// ...
$id = $this->user->getId(); // Some random/pseudorandom/regular number
$this->cleaner->autoclean($id, 'updated');
// OR
$this->cleaner->autoclean($id); // 'updated' is default
// or set const `$cleanDateField`
// $id - required
// if $id = 10 and `$autocleanEvery` = 10 - then cleanup will be called every time
// if $id not divisible by 10 and `$autocleanEvery` = 10 - cleanup will not be called
}
// ...
批量清理器
一些设置
public bool $batchCleanVerbose = false; // verbose output
public int $cleanBatchPart = 1000; // count deleted items on iteration
public int $batchItemsCount = 10000; // count all selected items
public int $batchLoopDelay = 0; // delay between iterations
// ...
public function someFunction()
{
// ...
$this->cleaner->batchclean('updated');
// OR
$this->cleaner->batchclean(); // 'updated' is default
// or set const `$cleanDateField`
}
// ...