Refactor form bulk action to docrine
[MAILPOET-3630]
This commit is contained in:
@@ -4,7 +4,6 @@ namespace MailPoet\Form;
|
||||
|
||||
use MailPoet\Doctrine\Repository;
|
||||
use MailPoet\Entities\FormEntity;
|
||||
use MailPoetVendor\Carbon\Carbon;
|
||||
|
||||
/**
|
||||
* @extends Repository<FormEntity>
|
||||
@@ -37,21 +36,56 @@ class FormsRepository extends Repository {
|
||||
}
|
||||
|
||||
public function delete(FormEntity $form) {
|
||||
$this->remove($form);
|
||||
$this->entityManager->remove($form);
|
||||
$this->flush();
|
||||
}
|
||||
|
||||
public function trash(FormEntity $form) {
|
||||
$this->updateDeletedAt($form, Carbon::now());
|
||||
$this->bulkTrash([$form->getId()]);
|
||||
$this->entityManager->refresh($form);
|
||||
}
|
||||
|
||||
public function restore(FormEntity $form) {
|
||||
$this->updateDeletedAt($form, null);
|
||||
$this->bulkRestore([$form->getId()]);
|
||||
$this->entityManager->refresh($form);
|
||||
}
|
||||
|
||||
private function updateDeletedAt(FormEntity $form, ?Carbon $value) {
|
||||
$form->setDeletedAt($value);
|
||||
$this->persist($form);
|
||||
$this->flush();
|
||||
public function bulkTrash(array $ids): int {
|
||||
if (empty($ids)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $this->entityManager->createQueryBuilder()
|
||||
->update(FormEntity::class, 'f')
|
||||
->set('f.deletedAt', 'CURRENT_TIMESTAMP()')
|
||||
->where('f.id IN (:ids)')
|
||||
->setParameter('ids', $ids)
|
||||
->getQuery()->execute();
|
||||
}
|
||||
|
||||
public function bulkRestore(array $ids): int {
|
||||
if (empty($ids)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $this->entityManager->createQueryBuilder()
|
||||
->update(FormEntity::class, 'f')
|
||||
->set('f.deletedAt', ':deletedAt')
|
||||
->where('f.id IN (:ids)')
|
||||
->setParameter('deletedAt', null)
|
||||
->setParameter('ids', $ids)
|
||||
->getQuery()->execute();
|
||||
}
|
||||
|
||||
public function bulkDelete(array $ids): int {
|
||||
if (empty($ids)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $this->entityManager->createQueryBuilder()
|
||||
->delete(FormEntity::class, 'f')
|
||||
->where('f.id IN (:ids)')
|
||||
->setParameter('ids', $ids)
|
||||
->getQuery()->execute();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user