Skip to content

Commit ea09e17

Browse files
Merge pull request #6730 from christianbeeznest/GH-6134-4
Assignment: Resolve child-entity error on delete - refs #6134
2 parents 624aae6 + 72861dd commit ea09e17

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

src/CoreBundle/State/CStudentPublicationDeleteProcessor.php

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,48 @@
88

99
use ApiPlatform\Metadata\Operation;
1010
use ApiPlatform\State\ProcessorInterface;
11-
use Chamilo\CoreBundle\Repository\TrackEDefaultRepository;
1211
use Chamilo\CourseBundle\Entity\CStudentPublication;
12+
use Chamilo\CoreBundle\Entity\ResourceNode;
1313
use Doctrine\ORM\EntityManagerInterface;
1414

1515
/**
1616
* @implements ProcessorInterface<CStudentPublication, void>
1717
*/
1818
final class CStudentPublicationDeleteProcessor implements ProcessorInterface
1919
{
20-
public function __construct(
21-
private readonly EntityManagerInterface $entityManager,
22-
private readonly TrackEDefaultRepository $trackRepo,
23-
) {}
24-
25-
public function process(
26-
$data,
27-
Operation $operation,
28-
array $uriVariables = [],
29-
array $context = []
30-
): void {
20+
public function __construct(private readonly EntityManagerInterface $em) {}
21+
22+
public function process($data, Operation $operation, array $uriVariables = [], array $context = []): void
23+
{
3124
if (!$data instanceof CStudentPublication) {
3225
return;
3326
}
3427

35-
if ($data->hasResourceNode()) {
36-
$this->trackRepo->registerResourceEvent(
37-
$data->getResourceNode(),
38-
'deletion'
39-
);
40-
}
28+
$node = $data->hasResourceNode() ? $data->getResourceNode() : null;
4129

42-
foreach ($data->getChildren() as $child) {
43-
$this->entityManager->remove($child);
44-
}
30+
$this->em->beginTransaction();
31+
try {
32+
try { $this->em->refresh($data); } catch (\Throwable) {}
33+
34+
$this->em->remove($data);
35+
$this->em->flush();
4536

46-
$this->entityManager->remove($data);
47-
$this->entityManager->flush();
37+
if ($node instanceof ResourceNode) {
38+
foreach ($node->getResourceLinks() as $link) {
39+
$this->em->remove($link);
40+
}
41+
$this->em->flush();
42+
43+
foreach ($node->getResourceFiles() as $file) {
44+
$this->em->remove($file);
45+
}
46+
$this->em->flush();
47+
}
48+
49+
$this->em->commit();
50+
} catch (\Throwable $e) {
51+
$this->em->rollback();
52+
throw $e;
53+
}
4854
}
4955
}

src/CourseBundle/Entity/CStudentPublication.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class CStudentPublication extends AbstractResource implements ResourceInterface,
147147
/**
148148
* @var Collection<int, CStudentPublication>
149149
*/
150-
#[ORM\OneToMany(mappedBy: 'publicationParent', targetEntity: self::class)]
150+
#[ORM\OneToMany(mappedBy: 'publicationParent', targetEntity: self::class, cascade: ['remove'], orphanRemoval: true)]
151151
protected Collection $children;
152152

153153
/**
@@ -168,7 +168,7 @@ class CStudentPublication extends AbstractResource implements ResourceInterface,
168168
protected User $user;
169169

170170
#[Groups(['c_student_publication:write', 'student_publication:read'])]
171-
#[ORM\OneToOne(mappedBy: 'publication', targetEntity: CStudentPublicationAssignment::class, cascade: ['persist'])]
171+
#[ORM\OneToOne(mappedBy: 'publication', targetEntity: CStudentPublicationAssignment::class, cascade: ['persist', 'remove'])]
172172
#[Assert\Valid]
173173
protected ?CStudentPublicationAssignment $assignment = null;
174174

src/CourseBundle/Entity/CStudentPublicationAssignment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class CStudentPublicationAssignment implements Stringable
5050
#[ORM\Column(name: 'enable_qualification', type: 'boolean', nullable: false)]
5151
protected bool $enableQualification;
5252

53-
#[ORM\OneToOne(inversedBy: 'assignment', targetEntity: CStudentPublication::class)]
53+
#[ORM\OneToOne(inversedBy: 'assignment', targetEntity: CStudentPublication::class, cascade: ['persist'])]
5454
#[ORM\JoinColumn(name: 'publication_id', referencedColumnName: 'iid', onDelete: 'CASCADE')]
5555
protected CStudentPublication $publication;
5656

0 commit comments

Comments
 (0)