diff --git a/src/CoreBundle/Entity/AccessUrlRelCourse.php b/src/CoreBundle/Entity/AccessUrlRelCourse.php index 637ed5f961..f906887c6e 100644 --- a/src/CoreBundle/Entity/AccessUrlRelCourse.php +++ b/src/CoreBundle/Entity/AccessUrlRelCourse.php @@ -25,11 +25,11 @@ class AccessUrlRelCourse implements EntityAccessUrlInterface, Stringable protected ?int $id = null; #[ORM\ManyToOne(targetEntity: Course::class, cascade: ['persist'], inversedBy: 'urls')] - #[ORM\JoinColumn(name: 'c_id', referencedColumnName: 'id')] - protected Course $course; + #[ORM\JoinColumn(name: 'c_id', referencedColumnName: 'id', nullable: false, onDelete: "CASCADE")] + protected ?Course $course; #[ORM\ManyToOne(targetEntity: AccessUrl::class, cascade: ['persist'], inversedBy: 'courses')] - #[ORM\JoinColumn(name: 'access_url_id', referencedColumnName: 'id')] + #[ORM\JoinColumn(name: 'access_url_id', referencedColumnName: 'id', nullable: false, onDelete: "CASCADE")] protected ?AccessUrl $url; public function __toString(): string @@ -54,7 +54,7 @@ class AccessUrlRelCourse implements EntityAccessUrlInterface, Stringable return $this; } - public function getCourse(): Course + public function getCourse(): ?Course { return $this->course; } diff --git a/src/CoreBundle/Entity/Course.php b/src/CoreBundle/Entity/Course.php index 8889e06801..8a26b40852 100644 --- a/src/CoreBundle/Entity/Course.php +++ b/src/CoreBundle/Entity/Course.php @@ -164,7 +164,8 @@ class Course extends AbstractResource implements cascade: [ 'persist', 'remove', - ] + ], + orphanRemoval: true )] protected Collection $tools; diff --git a/src/CoreBundle/Entity/CourseRelUser.php b/src/CoreBundle/Entity/CourseRelUser.php index 910a85c4a7..c0d8130ee0 100644 --- a/src/CoreBundle/Entity/CourseRelUser.php +++ b/src/CoreBundle/Entity/CourseRelUser.php @@ -68,7 +68,7 @@ class CourseRelUser implements Stringable #[Groups(['course_rel_user:read'])] #[ORM\ManyToOne(targetEntity: Course::class, inversedBy: 'users', cascade: ['persist'])] - #[ORM\JoinColumn(name: 'c_id', referencedColumnName: 'id')] + #[ORM\JoinColumn(name: 'c_id', referencedColumnName: 'id', onDelete: "CASCADE")] protected Course $course; #[Groups(['course:read', 'user:read'])] diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20230824115700.php b/src/CoreBundle/Migrations/Schema/V200/Version20230824115700.php new file mode 100644 index 0000000000..713571ee51 --- /dev/null +++ b/src/CoreBundle/Migrations/Schema/V200/Version20230824115700.php @@ -0,0 +1,50 @@ +getTable('access_url_rel_course'); + + if ($accessUrlRelCourse->hasForeignKey('FK_8E97FC0891D79BD3')) { + $this->addSql('ALTER TABLE access_url_rel_course DROP FOREIGN KEY FK_8E97FC0891D79BD3'); + } + + if ($accessUrlRelCourse->hasForeignKey('FK_8E97FC0873444FD5')) { + $this->addSql('ALTER TABLE access_url_rel_course DROP FOREIGN KEY FK_8E97FC0873444FD5'); + } + + $this->addSql('ALTER TABLE access_url_rel_course CHANGE c_id c_id INT NOT NULL, CHANGE access_url_id access_url_id INT NOT NULL'); + $this->addSql('ALTER TABLE access_url_rel_course ADD CONSTRAINT FK_8E97FC0891D79BD3 FOREIGN KEY (c_id) REFERENCES course (id) ON DELETE CASCADE'); + $this->addSql('ALTER TABLE access_url_rel_course ADD CONSTRAINT FK_8E97FC0873444FD5 FOREIGN KEY (access_url_id) REFERENCES access_url (id) ON DELETE CASCADE'); + + $courseRelUser = $schema->getTable('course_rel_user'); + + if ($courseRelUser->hasForeignKey('FK_92CFD9FE91D79BD3')) { + $this->addSql('ALTER TABLE course_rel_user DROP FOREIGN KEY FK_92CFD9FE91D79BD3'); + } + + $this->addSql('ALTER TABLE course_rel_user ADD CONSTRAINT FK_92CFD9FE91D79BD3 FOREIGN KEY (c_id) REFERENCES course (id) ON DELETE CASCADE'); + + $cTool = $schema->getTable('c_tool'); + + if ($cTool->hasForeignKey('FK_8456658091D79BD3')) { + $this->addSql('ALTER TABLE c_tool DROP FOREIGN KEY FK_8456658091D79BD3'); + } + + $this->addSql('ALTER TABLE c_tool ADD CONSTRAINT FK_8456658091D79BD3 FOREIGN KEY (c_id) REFERENCES course (id) ON DELETE CASCADE'); + } +} \ No newline at end of file diff --git a/src/CourseBundle/Entity/CTool.php b/src/CourseBundle/Entity/CTool.php index 02a16f7491..5ec7f55680 100644 --- a/src/CourseBundle/Entity/CTool.php +++ b/src/CourseBundle/Entity/CTool.php @@ -50,7 +50,7 @@ class CTool extends AbstractResource implements protected ?bool $visibility = null; #[ORM\ManyToOne(targetEntity: Course::class, inversedBy: 'tools')] - #[ORM\JoinColumn(name: 'c_id', referencedColumnName: 'id', nullable: false)] + #[ORM\JoinColumn(name: 'c_id', referencedColumnName: 'id', nullable: false, onDelete: "CASCADE")] #[Gedmo\SortableGroup] protected Course $course;