From cf6add41a7e8ce407b90400fc72fcbb51a0828f0 Mon Sep 17 00:00:00 2001
From: Jonathan Foucher <jfoucher@gmail.com>
Date: Fri, 15 Nov 2024 15:27:17 +0100
Subject: [PATCH] remove current item from self referencing linkable items.
 Fixes https://gitlab.adullact.net/soluris/madis/-/issues/970

---
 src/Application/Form/Type/LinkableType.php | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/Application/Form/Type/LinkableType.php b/src/Application/Form/Type/LinkableType.php
index 676761038..0e677ae3c 100644
--- a/src/Application/Form/Type/LinkableType.php
+++ b/src/Application/Form/Type/LinkableType.php
@@ -34,7 +34,7 @@ class LinkableType extends AbstractType
             'multiple'      => true,
             'expanded'      => false,
             'by_reference'  => false,
-            'query_builder' => function (EntityRepository $er) use ($object, $orderField) {
+            'query_builder' => function (EntityRepository $er) use ($object, $orderField, $class) {
                 $collectivity = null;
                 if (!\is_null($object) && $object instanceof CollectivityRelated && !\is_null($object->getCollectivity())) {
                     $collectivity = $object->getCollectivity();
@@ -51,9 +51,15 @@ class LinkableType extends AbstractType
                 } elseif ($orderField) {
                     $qb->addOrderBy('c.' . $orderField, 'asc');
                 }
+                if ($object instanceof $class) {
+                    // self referencing, remove current object from query
+                    $qb->andWhere('c != :self')
+                        ->setParameter('self', $object)
+                    ;
+                }
                 if ($collectivity) {
                     return $qb
-                        ->where('c.collectivity = :collectivity')
+                        ->andWhere('c.collectivity = :collectivity')
                         ->setParameter('collectivity', $collectivity)
                     ;
                 }
-- 
GitLab