Skip to content

[ObjectMapper] Add support for Doctrine entities #60610

@seb-jean

Description

@seb-jean

Description

Add support for Doctrine entities.
Currently I am encountering the following error:

A new entity was found through the relationship 'App\Entity\User#location' that was not configured to cascade persist operations for entity: App\Entity\Location@788. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example #[ORM\ManyToOne(..., cascade: ['persist'])]. If you cannot find out which entity causes the problem implement 'App\Entity\Location#__toString()' to get a clue.

Example

Hi,

I have a classic User entity:

<?php

namespace App\Entity;

[...]

#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: '`user`')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 180)]
    private ?string $email = null;

    #[ORM\Column]
    private ?string $password = null;

    #[ORM\ManyToOne]
    #[ORM\JoinColumn(nullable: false)]
    private ?Location $location = null;
	
    [...]
}

As well as another entity Location:

<?php

namespace App\Entity;

[...]

#[ORM\Entity(repositoryClass: LocationRepository::class)]
class Location
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column]
    private string $cityName;

    #[ORM\Column(length: 255)]
    private string $countryName;
	
    [...]
}

The Registration DTO is linked to the User entity. This DTO has the $location property which represents a Location entity.

<?php

namespace App\Form\Model;

[...]

#[Map(target: User::class)]
final class Registration
{
    public ?string $email = null;
    public ?string $plainPassword;
    public ?Location $location;

    [...]
}

Finally, here is the controller that I use to map the Dto Registration to the User entity:

<?php

namespace App\Controller\User;

[...]

final class RegistrationController extends AbstractController
{
    #[Route('/register', methods: ['GET', 'POST'])]
    public function register(
        #[CurrentUser] ?User $user,
        Request $request,
        UserPasswordHasherInterface $userPasswordHasher,
        EntityManagerInterface $entityManager,
        RegistrationSession $registrationSession,
        ObjectMapperInterface $objectMapper,
        RegistrationStep $registrationStep = RegistrationStep::Email,
    ): ?Response {
        $registration = $registrationSession->start();

        $form = $this->createForm(RegistrationForm::class, $registration, ['step' => $registrationStep]);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $registrationSession->save($form->getData());

            if (Registration::isFinalStep($registrationStep)) {
                $user = new User();
                $objectMapper->map($registration, $user);

                /** @var string $plainPassword */
                $plainPassword = $registration->plainPassword;

                // encode the plain password
                $user->setPassword($userPasswordHasher->hashPassword($user, $plainPassword));

                $entityManager->persist($user);
                $entityManager->flush();

                $registrationSession->terminate();
            }
        }

        return $this->render(\sprintf('user/registration/register/%s.html.twig', u($registrationStep->value)->snake()), $parameters);
    }
}

The problem is when I persist then flush, I get the following error:

A new entity was found through the relationship 'App\Entity\User#location' that was not configured to cascade persist operations for entity: App\Entity\Location@788. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example #[ORM\ManyToOne(..., cascade: ['persist'])]. If you cannot find out which entity causes the problem implement 'App\Entity\Location#__toString()' to get a clue.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy