diff --git a/src/CoreBundle/Entity/CourseRelUser.php b/src/CoreBundle/Entity/CourseRelUser.php index bb3baff792..7a9e3356a9 100644 --- a/src/CoreBundle/Entity/CourseRelUser.php +++ b/src/CoreBundle/Entity/CourseRelUser.php @@ -60,12 +60,12 @@ class CourseRelUser implements Stringable protected ?int $id = null; #[Groups(['course:read', 'user:read', 'course_rel_user:read'])] - #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'courses', cascade: ['persist'])] - #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id')] + #[ORM\ManyToOne(targetEntity: User::class, cascade: ['persist'], inversedBy: 'courses')] + #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', onDelete: 'CASCADE')] protected User $user; #[Groups(['course_rel_user:read'])] - #[ORM\ManyToOne(targetEntity: Course::class, inversedBy: 'users', cascade: ['persist'])] + #[ORM\ManyToOne(targetEntity: Course::class, cascade: ['persist'], inversedBy: 'users')] #[ORM\JoinColumn(name: 'c_id', referencedColumnName: 'id', onDelete: 'CASCADE')] protected Course $course; diff --git a/src/CoreBundle/Entity/TrackEAttemptQualify.php b/src/CoreBundle/Entity/TrackEAttemptQualify.php index 0582f61f01..27b670d634 100644 --- a/src/CoreBundle/Entity/TrackEAttemptQualify.php +++ b/src/CoreBundle/Entity/TrackEAttemptQualify.php @@ -53,7 +53,7 @@ class TrackEAttemptQualify protected ?string $answer; #[ORM\ManyToOne(inversedBy: 'revisedAttempts')] - #[ORM\JoinColumn(name: 'exe_id', referencedColumnName: 'exe_id', nullable: false)] + #[ORM\JoinColumn(name: 'exe_id', referencedColumnName: 'exe_id', nullable: false, onDelete: 'CASCADE')] private ?TrackEExercise $trackExercise = null; public function __construct() diff --git a/src/CoreBundle/EventSubscriber/AnonymousUserSubscriber.php b/src/CoreBundle/EventSubscriber/AnonymousUserSubscriber.php index 7948785e1b..d7c49f4fa0 100644 --- a/src/CoreBundle/EventSubscriber/AnonymousUserSubscriber.php +++ b/src/CoreBundle/EventSubscriber/AnonymousUserSubscriber.php @@ -6,8 +6,6 @@ declare(strict_types=1); namespace Chamilo\CoreBundle\EventSubscriber; -use Chamilo\CoreBundle\Entity\TrackEAttemptQualify; -use Chamilo\CoreBundle\Entity\TrackEExercise; use Chamilo\CoreBundle\Entity\TrackELogin; use Chamilo\CoreBundle\Entity\User; use Chamilo\CoreBundle\Settings\SettingsManager; @@ -125,8 +123,6 @@ class AnonymousUserSubscriber implements EventSubscriberInterface if ($oldestAnonymousUser) { error_log('Deleting oldest anonymous user: '.$oldestAnonymousUser->getId()); - $this->handleRelatedEntities($oldestAnonymousUser); - $this->entityManager->remove($oldestAnonymousUser); $this->entityManager->flush(); } @@ -158,30 +154,4 @@ class AnonymousUserSubscriber implements EventSubscriberInterface return $anonymousUser->getId(); } - - private function handleRelatedEntities(User $user): void - { - $trackEExercisesRepository = $this->entityManager->getRepository(TrackEExercise::class); - - $exercises = $trackEExercisesRepository->findBy(['user' => $user->getId()]); - - foreach ($exercises as $exercise) { - $this->handleAttemptRecordings($exercise); - - $this->entityManager->remove($exercise); - } - - $this->entityManager->flush(); - } - - private function handleAttemptRecordings(TrackEExercise $exercise): void - { - $trackEAttemptRecordingRepository = $this->entityManager->getRepository(TrackEAttemptQualify::class); - - $attemptRecordings = $trackEAttemptRecordingRepository->findBy(['trackExercise' => $exercise->getExeId()]); - - foreach ($attemptRecordings as $attemptRecording) { - $this->entityManager->remove($attemptRecording); - } - } }