Object does not bind correctly

I have the following relationships:

Presupuesto: oneToMany with Revision Revision: oneToMany with Capitulo Capitulo: oneToMany with Requisito Requisito: oneToMany with Articulo 

I have a form embedded with these entities and a set of 'addTagForm'. The problem is that when you submit the form, everything is correctly connected, with the exception of Revision with Capitulo , which is associated with null.

The following is the most important information about these two objects:

Revision.php

 /** * @ORM\OneToMany(targetEntity="CeiferIT\ComercialBundle\Entity\Capitulo", mappedBy="revision", cascade={"persist"}, orphanRemoval=true) */ protected $capitulos; /** * @param \CeiferIT\ComercialBundle\Entity\Capitulo $capitulo * * @return Revision */ public function addCapitulo(\CeiferIT\ComercialBundle\Entity\Capitulo $capitulo) { $capitulo->setRevision($this); $this->capitulos[] = $capitulo; return $this; } /** * @param \CeiferIT\ComercialBundle\Entity\Capitulo $capitulo */ public function removeCapitulo(\CeiferIT\ComercialBundle\Entity\Capitulo $capitulo) { $this->capitulos->removeElement($capitulo); } /** * @return \Doctrine\Common\Collections\Collection */ public function getCapitulos() { return $this->capitulos; } 

Capitulo.php

 /** * @ORM\ManyToOne(targetEntity="CeiferIT\ComercialBundle\Entity\Revision", inversedBy="capitulos", cascade={"persist"}) * @ORM\JoinColumn(name="revision_id", referencedColumnName="id") */ private $revision; /** * @param \CeiferIT\ComercialBundle\Entity\Revision $revision * * @return Capitulo */ public function setRevision(\CeiferIT\ComercialBundle\Entity\Revision $revision = null) { $this->revision = $revision; return $this; } /** * @return \CeiferIT\ComercialBundle\Entity\Revision */ public function getRevision() { return $this->revision; } 

nuevo.html.twig

 {{ form_start(formulario) }} //some code.. {% include 'ComercialBundle:Presupuesto:listaRevisiones.html.twig' %} //some code.. {{ form_end(formulario) }} 

listaRevisiones.html.twig

 {% macro information_prototype(revision) %} {% if form_errors(revision.total) %} {{ form_widget(revision.total, {'attr': {'class': 'form-control totalrevision error'}}) }} {% else %} {{ form_widget(revision.total, {'attr': {'class': 'form-control totalrevision'}}) }} {% endif %} {% include 'ComercialBundle:Presupuesto:listacapitulos.html.twig' %} {% endmacro %} <div class="ibox product-box active primerarevision" data-prototype="{{ _self.information_prototype(formulario.revisiones.vars.prototype)|e}}"> {% for revision in formulario.revisiones %} {{ _self.information_prototype(revision)}} {% endfor %} </div> 

I cannot understand why revision_id is null. Any ideas? Thanks

0
source share
1 answer

You are working on a Revision object that does not own the party to this relationship, so by default it will not be verified and saved by Doctrine.

It is important that you work on the entity on the user side (one with JoinColumn ).

Add to the controller (after checking the form) this:

 $capitulo->setRevision($revision); $em->flush(); 

More details here: http://docs.doctrine-project.org/en/latest/reference/unitofwork-associations.html#important-concepts

+1
source

Source: https://habr.com/ru/post/1243836/


All Articles