diff --git a/application/Espo/Jobs/Cleanup.php b/application/Espo/Jobs/Cleanup.php index 5352ff96ff..d1ca085757 100644 --- a/application/Espo/Jobs/Cleanup.php +++ b/application/Espo/Jobs/Cleanup.php @@ -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); diff --git a/application/Espo/SelectManagers/Attachment.php b/application/Espo/SelectManagers/Attachment.php index 65d2ec721e..859e6bba09 100644 --- a/application/Espo/SelectManagers/Attachment.php +++ b/application/Espo/SelectManagers/Attachment.php @@ -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', [