Data entry in database with symfony

0

Excuse me, I have a question about how to insert data in two tables with symfony. The fact is that I have a user table and a person table with a 1 to 1 ratio so to insert a user we need a person.

someone who can guide me on the insertion of data with symfony. I have already created my entities. but now my question is how would the code in the controller to make this insertion. and how it is done to bring the data of a user and his associated person to show them in front end Is a view necessary? This I have already done in Java. But in PHP I'm really new and more with symfony. The insertions that only entail a class in symfony do not present a problem to me but here if I really run out of ideas. Thanks for your answers

    
asked by Christopher Bazaldua 18.12.2018 в 12:16
source

1 answer

0

It does not change much with respect to the insertion of an unrelated entity. Just add the Entity related to the object. For example, the user must add the person, or add the user to the person (depends on how the relationship is configured). Then Doctrine is responsible for saving both objects in the database.

$entityManager = $this->getDoctrine()->getManager();
$usuario = new Usuario;
$persona = new Persona;

$usuario->setPersona($persona);
$entityManager->persist($usuario);
$entityManager->flush();

How to bring a user's data

You can bring the data of one of the entities and then you get the other, through the relationship. So:

$usuario = $this->getDoctrine()
    ->getRepository(Usuario::class)
    ->find($id);

$persona = $usuario->getPersona();

Then you can send the variables $ user and $ person to the twig template. For more information, I recommend seeing the official documentation

    
answered by 21.12.2018 в 16:50