cleanupOrphanAttachments fix

This commit is contained in:
Yuri Kuznetsov
2020-04-15 09:36:19 +03:00
parent f4c8931d7c
commit d6b992ef50
2 changed files with 28 additions and 28 deletions

View File

@@ -209,25 +209,17 @@ class Cleanup extends \Espo\Core\Jobs\Base
}
if ($this->getConfig()->get('cleanupOrphanAttachments')) {
$collection = $this->getEntityManager()->getRepository('Attachment')->where([
[
'role' => 'Attachment',
],
'OR' => [
[
'parentId' => null,
'parentType!=' => null,
'relatedType=' => null,
],
[
'parentType' => null,
'relatedId' => null,
'relatedType!=' => null,
]
],
$selectManager = $this->getContainer()->get('selectManagerFactory')->create('Attachment');
$selectParams = $selectManager->getEmptySelectParams();
$selectManager->applyFilter('orphan', $selectParams);
$selectParams['whereClause'][] = [
'createdAt<' => $datetime->format('Y-m-d H:i:s'),
'createdAt>' => '2018-01-01 00:00:00',
])->limit(0, 5000)->find();
];
$collection = $this->getEntityManager()->getRepository('Attachment')->limit(0, 5000)->find($selectParams);
foreach ($collection as $e) {
$this->getEntityManager()->removeEntity($e);

View File

@@ -35,19 +35,27 @@ class Attachment extends \Espo\Core\SelectManagers\Base
{
$result['whereClause'][] = [
'role' => ['Attachment', 'Inline Attachment'],
'OR' => [
[
'parentId' => null,
'parentType!=' => null,
'relatedType=' => null
[
'OR' => [
[
'parentId' => null,
'parentType!=' => null,
'relatedType=' => null,
],
[
'parentType' => null,
'relatedId' => null,
'relatedType!=' => null,
],
],
[
'parentType' => null,
'relatedId' => null,
'relatedType!=' => null
]
],
'attachmentChild.id' => null
[
'OR' => [
'relatedType!=' => 'Settings',
'relatedType=' => null,
],
],
'attachmentChild.id' => null,
];
$this->addLeftJoin(['Attachment', 'attachmentChild', [