Entity doesn't associate correctly

2019-09-10 05:28发布

问题:

I have the next 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 'addTagForm' set. The problem is that when I submit the form, everything is associated correctly except from Revision with Capitulo, which associates as null.

Below there's the most relevant information about these two entities:

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 figure out why revision_id is null. Any ideas? Thanks

回答1:

You work on Revision object which is not owning side of that relation so by default it will not be checked and persisted by Doctrine.

It's important that you work on owning-side entity (one with JoinColumn).

Add to your controller (after validating form) this:

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

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