@@ -84,26 +84,47 @@ public function prePersist(AbstractResource $resource, PrePersistEventArgs $even
8484 }
8585
8686 // 2. Set creator.
87- // Check if creator was set with $resource->setCreator()
87+ // Priority order:
88+ // 1) Resource node creator (if explicitly provided)
89+ // 2) Resource creator (set via $resource->setCreator())
90+ // 3) Current authenticated user (Security token)
91+ // Then normalize to a managed reference to avoid duplicate INSERT on user.username.
8892 $ creator = $ resource ->getResourceNodeCreator ();
93+ if (!$ creator instanceof User) {
94+ $ creator = null ;
95+ }
96+
97+ if (null === $ creator ) {
98+ $ explicitCreator = $ resource ->getCreator ();
99+ if ($ explicitCreator instanceof User) {
100+ $ creator = $ explicitCreator ;
101+ }
102+ }
89103
90- $ currentUser = null ;
91104 if (null === $ creator ) {
92- // Get the creator from the current request.
93- /** @var User|null $currentUser */
94105 $ currentUser = $ this ->security ->getUser ();
95- if (null !== $ currentUser ) {
106+ if ($ currentUser instanceof User ) {
96107 $ creator = $ currentUser ;
97108 }
109+ }
98110
99- // Check if user has a resource node.
100- if ($ resource ->hasResourceNode () && null !== $ resource ->getCreator ()) {
101- $ creator = $ resource ->getCreator ();
111+ if (!$ creator instanceof User) {
112+ throw new UserNotFoundException ('User creator not found, use $resource->setCreator(); ' );
113+ }
114+
115+ // Ensure creator is managed by this EntityManager.
116+ if (!$ em ->contains ($ creator )) {
117+ $ creatorId = $ creator ->getId ();
118+ if (null === $ creatorId ) {
119+ throw new UserNotFoundException ('Invalid creator user entity (missing ID). ' );
102120 }
121+
122+ $ creator = $ em ->getReference (User::class, (int ) $ creatorId );
103123 }
104124
105- if (null === $ creator ) {
106- throw new UserNotFoundException ('User creator not found, use $resource->setCreator(); ' );
125+ // Ensure the resource itself has creator set (not only the ResourceNode).
126+ if (null === $ resource ->getCreator ()) {
127+ $ resource ->setCreator ($ creator );
107128 }
108129
109130 // 3. Set ResourceType.
@@ -341,9 +362,6 @@ public function updateResourceName(AbstractResource $resource): void
341362 if (empty ($ extension )) {
342363 // $slug = $this->slugify->slugify($resourceName);
343364 }
344- /*$originalExtension = pathinfo($resourceName, PATHINFO_EXTENSION);
345- $originalBasename = \basename($resourceName, $originalExtension);
346- $slug = sprintf('%s.%s', $this->slugify->slugify($originalBasename), $originalExtension);*/
347365 $ resource ->getResourceNode ()->setTitle ($ resourceName );
348366 }
349367
0 commit comments