- I have a project in Symfony.
I create a form for a new entity.
class EquipoType extends AbstractType{ /** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $equipo = new Equipo(); $builder ->add('unidades', IntegerType::class) ->add('accionComercial', EntityType::class, array( 'class' => 'AppBundle\Entity\IndicoGpt\adelantoequipos\AccionComercial', 'em' => 'adeq', 'choices' => $equipo->getAccionComercial(), 'choice_label' => 'nombre', 'placeholder' => 'Elija Accion Comercial' )) ->add('modelo', TextType::class) ->add('save', SubmitType::class, array( 'label' => 'Guardar Equipo', 'attr' => array( 'class' => "btn btn-info", 'style' => "margin-top: 7px; margin-left:40%;" )) ); $builder->get('modelo') ->addModelTransformer(new CallbackTransformer( function ($modelo) { return $modelo ? $modelo->getModDescModelo() : $modelo; }, function ($modelo) use ($options) { return $modelo ? $options['em']->getRepository(ModModelo::class)->findOneBy(array( 'modDescModelo' => $modelo )) : $modelo; } )); public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\IndicoGpt\adelantoequipos\Equipo', 'em' => "adeq" )); } /** * {@inheritdoc} */ public function getBlockPrefix() { return 'appbundle_indicogpt_adelantoequipos_equipo'; } }
This entity has a relationship. (A Team has a model, a model can be of many Teams)
class Equipo { /** * @var \AppBundle\Entity\IndicoGpt\catalogo\ModModelo * * @ORM\ManyToOne(targetEntity="AppBundle\Entity\IndicoGpt\catalogo\ModModelo") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="modelo_id", referencedColumnName="MOD_ID_MODELO") * }) */ private $modelo;
The models are already in the DB, that is. That a team should only save the existing id of a Model. Controller:
public function crearEquipo(Request $request){ $emAdeq = $this->getDoctrine()->getManager('adeq'); $emCatalogo = $this->getDoctrine()->getManager('catalogo'); $equipo = new Equipo(); $form = $this->createForm(EquipoType::class, $equipo, array( 'action' => "crearEquipo", 'em' => $emCatalogo, 'attr' => array( 'class' => "form" ) )); $form->handleRequest($request); if ($form->isSubmitted()) { $equipo = $form->getData(); $emAdeq->persist($equipo); $emAdeq->flush(); $this->addFlash('exito', $equipo->getId()); return $this->redirectToRoute('portadaAdelantoEquipos'); } return $this->render('AppBundle:AdelantoEquipos/forms:adelantosForm.html.twig', array( 'form' => $form->createView() ));
}
When I go to Save the new Team, it tells me that it has found a new entity in the relationship, but that entity is not new, it is an existing model that is now related to the Team. As can be seen in the image, I choose a model from the existing ones through an autocomplete field.
Just before doing the Persist of the entity, I can see in the debugger the object that I am going to record and it appears well composed:
The error is the following:
A new entity was found through the relationship 'AppBundle\Entity\IndicoGpt\adelantoequipos\Equipo#model' that was not configured to cascade persist operations for entity: AppBundle\Entity\IndicoGpt\catalogo\ModModelo@0000000011088ca600000000661c7dca. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'AppBundle\Entity\IndicoGpt\catalogo\ModModelo#__toString()' to get a clue.
I don't understand why it has a numeric code @0000000012ce55020000000059452bb5 or why it thinks it is a new entity.
Does anyone know what behavior Doctrine is having?
How can I record the Team entity with its relationship?
I have seen similar problems solved by resetting the objects by extracting them from the database, but that involves setting too many relationships. Here
The problem is that I was using two entityManager since the relation is made in two different databases.
But I haven't figured out that the related entity has its database set so I just need an EntityManager. Since with one he gives me back the relationships
Having two entityManagers asked me to record twice.
Solution: Establish your DB in the entity:
*@ORM\Table(name="adelantoequipos.adelanto"
Where I have used to$emCatalgo
replace it with$emAdeq
and eliminate the one from the catalog since it is useless.Friend that poster tells you in detail the problem you have. What happens is that implementation errors can occur when accessing the Model in the Team entity and it also tells you to try this to solve it:
add cascade={"persist"} where you declare the relationship.