Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3925

How to change parent entity on inherited entity in Symfony4 Doctrine

$
0
0

I have multiple entity type which extends each other in order to create my model. In my application I need to manage Interimaries, Employes and Users.

An Interimary is working in a company, as is an Employe. A user in an Employe of my application with an access to the application.

My model is defined as follow :

Worker :

<?php

/**
 * @ORM\Entity(repositoryClass="App\Repository\User\WorkerRepository")
 * @ORM\Table(name="usr_worker")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({"worker" = "Worker", "employe" = "Employe", "interimary" = "Interimary", "user" = "User"})
 */
class Worker
{
    private $id;
}

Interimary :

<?php

/**
 * @ORM\Entity(repositoryClass="App\Repository\User\InterimaryRepository")
 * @ORM\Table(name="usr_interimary")
 */
class Interimary extends Worker{}

Employe :

<?php

/**
 * @ORM\Entity(repositoryClass="App\Repository\User\EmployeRepository")
 * @ORM\Table(name="usr_employe")
 */
class Employe extends Worker{}

User :

<?php

/**
 * @ORM\Entity(repositoryClass="App\Repository\User\UserRepository")
 * @ORM\Table(name="usr_user")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
 * @ORM\HasLifecycleCallbacks()
 */
class User extends Employe implements UserInterface
{
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $username;


    /**
     * @var string The hashed password
     * @ORM\Column(type="string", nullable=true)
     */
    private $password;
}

All my authentication logic is in my User class. The logic of my application is that when a user first login, he must select in a dropdown the Employe he is attached to.

Problem : So I have an Employe entity and a User entity I want to "merge" when the user select the employe.

Solution ? : My first idea was to change the User ID to fit my Employe ID, and then delete the old User's Employe but this is not working. I'm not sure the approach is bad but I have no idea about what is not working with this method.

/**
 * @Route("/user/linkemploye", name="user_employe_link")
 */
public function linkUserEmploye(Request $request)
{
    $user       = $this->getUser();
    $employes   = $this->employeRepository->findUnlincked();

    if($request->isMethod('POST')){

        $idEmploye = $request->request->get('employe');

        $employe        = $this->employeRepository->find($user->getId());
        $intoEmploye    = $this->employeRepository->find($idEmploye);

        $this->mergeEmployes($employe, $intoEmploye);

        $user->setUserLincked(true);

        $em = $this->getDoctrine()->getManager();
        $em->persist($user);
        $em->flush();

        return $this->redirectToRoute('app_homepage');
    }

    return $this->render('app/users/security/UserEmployeLink/userEmployeLink.html.twig', [
        "employes" => $employes
    ]);
}

public function mergeEmployes(Employe $employe, Employe $intoEmploye){
    //How to properly merge my 2 entities ?
}

Viewing all articles
Browse latest Browse all 3925

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>