Entity doesn't associate correctly

寵の児 提交于 2019-12-25 04:39:12

问题


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



来源:https://stackoverflow.com/questions/35578004/entity-doesnt-associate-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!