Skip to content

Commit b084690

Browse files
committed
Refactor delete method to use EntityManager for cascading deletions of related access URLs - refs BT#22639
1 parent ff362b7 commit b084690

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

public/main/inc/lib/urlmanager.lib.php

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
/* For licensing terms, see /license.txt */
44

55
use Chamilo\CoreBundle\Entity\AccessUrl;
6+
use Chamilo\CoreBundle\Entity\AccessUrlRelCourse;
7+
use Chamilo\CoreBundle\Entity\AccessUrlRelSession;
8+
use Chamilo\CoreBundle\Entity\AccessUrlRelUser;
9+
use Chamilo\CoreBundle\Entity\AccessUrlRelUserGroup;
610
use Chamilo\CoreBundle\Framework\Container;
711

812
/**
@@ -90,27 +94,44 @@ public static function update($urlId, $url, $description, $active, bool $isLogin
9094
public static function delete($id)
9195
{
9296
$id = (int) $id;
93-
$table = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
94-
$tableUser = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
95-
$tableCourse = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
96-
$tableSession = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
97-
$tableGroup = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP);
9897

99-
$sql = "DELETE FROM $tableCourse WHERE access_url_id = ".$id;
100-
Database::query($sql);
10198
/*
10299
* $tableCourseCategory = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
103100
$sql = "DELETE FROM $tableCourseCategory WHERE access_url_id = ".$id;
104101
Database::query($sql);
105102
*/
106-
$sql = "DELETE FROM $tableSession WHERE access_url_id = ".$id;
107-
Database::query($sql);
108-
$sql = "DELETE FROM $tableGroup WHERE access_url_id = ".$id;
109-
Database::query($sql);
110-
$sql = "DELETE FROM $tableUser WHERE access_url_id = ".$id;
111-
Database::query($sql);
112-
$sql = "DELETE FROM $table WHERE id = ".$id;
113-
Database::query($sql);
103+
$em = Container::getEntityManager();
104+
105+
$relEntities = [
106+
AccessUrlRelCourse::class,
107+
AccessUrlRelSession::class,
108+
AccessUrlRelUserGroup::class,
109+
AccessUrlRelUser::class,
110+
];
111+
112+
foreach ($relEntities as $relEntity) {
113+
$qb = $em->createQueryBuilder();
114+
115+
$em
116+
->createQueryBuilder()
117+
->delete($relEntity, 'rel')
118+
->where($qb->expr()->eq('rel.url', ':id'))
119+
->setParameter('id', $id)
120+
->getQuery()
121+
->execute()
122+
;
123+
}
124+
125+
$qb = $em->createQueryBuilder();
126+
127+
$em
128+
->createQueryBuilder()
129+
->delete(AccessUrl::class, 'au')
130+
->where($qb->expr()->eq('au.id', ':id'))
131+
->setParameter('id', $id)
132+
->getQuery()
133+
->execute()
134+
;
114135

115136
return true;
116137
}

0 commit comments

Comments
 (0)