@@ -58,6 +58,20 @@ public function process(
5858 $ currentUser = null ;
5959 }
6060
61+ // Ensure we always assign a managed User reference BEFORE any persist/flush happens.
62+ // This prevents Doctrine from treating the User as a new/unknown entity.
63+ $ targetUserId = null ;
64+ if ($ isUpdate && $ originalUser instanceof User && null !== $ originalUser ->getId ()) {
65+ $ targetUserId = $ originalUser ->getId ();
66+ } elseif (!$ isUpdate && $ currentUser instanceof User && null !== $ currentUser ->getId ()) {
67+ $ targetUserId = $ currentUser ->getId ();
68+ }
69+
70+ if (null !== $ targetUserId ) {
71+ $ publication ->setUser ($ this ->entityManager ->getReference (User::class, $ targetUserId ));
72+ }
73+
74+ // Persist/flush (ApiPlatform default processor)
6175 $ result = $ this ->persistProcessor ->process ($ publication , $ operation , $ uriVariables , $ context );
6276
6377 $ assignment = $ publication ->getAssignment ();
@@ -116,16 +130,6 @@ public function process(
116130 }
117131
118132 $ publication ->setViewProperties (true );
119- if (!$ isUpdate ) {
120- if ($ currentUser instanceof User) {
121- $ publication ->setUser ($ currentUser );
122- }
123- } else {
124- if ($ originalUser instanceof User) {
125- $ publication ->setUser ($ originalUser );
126- }
127- }
128-
129133 $ this ->entityManager ->flush ();
130134
131135 $ this ->saveGradebookConfig ($ publication , $ course , $ session );
@@ -178,11 +182,16 @@ private function saveCalendarEvent(
178182 $ color = $ agendaColors ['student_publication ' ];
179183 }
180184
185+ $ creator = $ publication ->getCreator ();
186+ if ($ creator instanceof User && null !== $ creator ->getId ()) {
187+ $ creator = $ this ->entityManager ->getReference (User::class, $ creator ->getId ());
188+ }
189+
181190 $ event = (new CCalendarEvent ())
182191 ->setTitle ($ eventTitle )
183192 ->setContent ($ content )
184193 ->setParent ($ course )
185- ->setCreator ($ publication -> getCreator () )
194+ ->setCreator ($ creator )
186195 ->addLink (clone $ courseLink )
187196 ->setStartDate ($ startDate )
188197 ->setEndDate ($ endDate )
0 commit comments