Problemas con la persistencia de objetos

27 views
Skip to first unread message

Jorge Martin

unread,
Sep 4, 2014, 11:38:28 AM9/4/14
to symfony_...@googlegroups.com
Hola a todos. Como algunos ya sabeis he aterrizado en un proyecto con Symfony2 del que no tengo ni idea. Poco a poco he ido resolviendo las cosillas que han ido surgiendo, pero ahora tengo un problema que a buen seguro, los que llevais muchas horas con el Symfony2 podreis resolverme y que yo no encuentro como hacerlo.
Tengo las siguientes entidades:

<?php

namespace RM\DiscretasBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * VidGrupoSegmento
 *
 * @ORM\Table(name="vid_grupo_segmento")
 * @ORM\Entity
 */
class VidGrupoSegmento {
   
    /**
     * @var integer
     *
     * @ORM\Column(name="id_vid_grupo_segmento", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $idVidGrupoSegmento;
   
    /**
     * @var RM\DiscretasBundle\Entity\Vid
     *
     * @ORM\ManyToOne(targetEntity="RM\DiscretasBundle\Entity\Vid")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="id_vid", referencedColumnName="id_vid")
     * })
     */
    private $idVid;
   
    /**
     * @var \RM\ProductoBundle\Entity\Marca
     *
     * @ORM\ManyToOne(targetEntity="RM\ProductoBundle\Entity\Marca")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="id_marca", referencedColumnName="id_marca")
     * })
     */
    private $idMarca;
   
    /**
     * @var \RM\DiscretasBundle\Entity\Categoria
     *
     * @ORM\ManyToOne(targetEntity="RM\CategoriaBundle\Entity\Categoria")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="id_categoria", referencedColumnName="id_categoria")
     * })
     */
    private $idCategoria;
   
    /**
     * @var integer
     *
     * @ORM\Column(name="meses_n", type="integer", nullable=true)
     */
    private $mesesN;

    /**
     * @var integer
     *
     * @ORM\Column(name="meses_m", type="integer", nullable=true)
     */
    private $mesesM;

    /**
     * @var float
     *
     * @ORM\Column(name="desviacion_max", type="float", nullable=true)
     */
    private $desviacionMax;

    /**
     * @var smallint
     *
     * @ORM\Column(name="estado", type="smallint", nullable=true)
     */
    private $estado;
   

    /**
     * Set estado
     *
     * @param smallint $estado
     * @return VidGrupoSegmento
     */
    public function setEstado($estado)
    {
        $this->estado = $estado;

        return $this;
    }

    /**
     * Get estado
     *
     * @return smallint
     */
    public function getEstado()
    {
        return $this->estado;
    }

    /**
     * Get VidGrupoSegmento
     *
     * @return integer
     */
    public function getIdVidGrupoSegmento()
    {
        return $this->idVidGrupoSegmento;
    }

    /**
     * Set idVid
     *
     * @param \RM\DiscretasBundle\Entity\Vid $idVid
     * @return VidGrupoSegmento
     */
    public function setIdVid(\RM\DiscretasBundle\Entity\Vid $idVid = null)
    {
        $this->idVid = $idVid;

        return $this;
    }

    /**
     * Get idVid
     *
     * @return \RM\DiscretasBundle\Entity\Vid
     */
    public function getIdVid()
    {
        return $this->idVid;
    }

    /**
     * Set idMarca
     *
     * @param \RM\ProductoBundle\Entity\Marca $idMarca
     * @return VidGrupoSegmento
     */
    public function setIdMarca(\RM\ProductoBundle\Entity\Marca $idMarca = null)
    {
        $this->idMarca = $idMarca;

        return $this;
    }

    /**
     * Get idMarca
     *
     * @return \RM\ProductoBundle\Entity\Marca
     */
    public function getIdMarca()
    {
        return $this->idMarca;
    }

    /**
     * Set idCategoria
     *
     * @param \RM\CategoriaBundle\Entity\Categoria $idCategoria
     * @return VidGrupoSegmento
     */
    public function setIdCategoria(\RM\CategoriaBundle\Entity\Categoria $idCategoria = null)
    {
        $this->idCategoria = $idCategoria;

        return $this;
    }

    /**
     * Get idCategoria
     *
     * @return \RM\CategoriaBundle\Entity\Categoria
     */
    public function getIdCategoria()
    {
        return $this->idCategoria;
    }
   
    /**
     * Set mesesN
     *
     * @param integer $mesesN
     * @return VidGrupoSegmento
     */
    public function setMesesN($mesesN)
    {
        $this->mesesN = $mesesN;
   
        return $this;
    }
   
    /**
     * Get mesesN
     *
     * @return integer
     */
    public function getMesesN()
    {
        return $this->mesesN;
    }
   
    /**
     * Set mesesM
     *
     * @param integer $mesesM
     * @return VidGrupoSegmento
     */
    public function setMesesM($mesesM)
    {
        $this->mesesM = $mesesM;
   
        return $this;
    }
   
    /**
     * Get mesesM
     *
     * @return integer
     */
    public function getMesesM()
    {
        return $this->mesesM;
    }
   
    /**
     * Set desviacionMax
     *
     * @param float $desviacionMax
     * @return VidGrupoSegmento
     */
    public function setDesviacionMax($desviacionMax)
    {
        $this->desviacionMax = $desviacionMax;
   
        return $this;
    }
   
    /**
     * Get desviacionMax
     *
     * @return float
     */
    public function getDesviacionMax()
    {
        return $this->desviacionMax;
    }
}

------------------------------
--------------------------------------------------------------------------------------------

<?php

namespace RM\DiscretasBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Vid
 *
 * @ORM\Table(name="vid")
 * @ORM\Entity(repositoryClass="RM\DiscretasBundle\Entity\VidRepository")
 */
class Vid
{
    /**
     * @var string
     *
     * @ORM\Column(name="nombre", type="string", length=255, nullable=true)
     */
    private $nombre;

    /**
     * @var string
     *
     * @ORM\Column(name="descripcion", type="string", length=255, nullable=true)
     */
    private $descripcion;

    /**
     * @var smallint
     *
     * @ORM\Column(name="tipo", type="smallint", nullable=true)
     */
    private $tipo;

    /**
     * @var smallint
     *
     * @ORM\Column(name="clasificacion", type="smallint", nullable=true)
     */
    private $clasificacion;

    /**
     * @var smallint
     *
     * @ORM\Column(name="solicita_tiempo", type="smallint", nullable=true)
     */
    private $solicitaTiempo;   

    /**
     * @var smallint
     *
     * @ORM\Column(name="estado", type="smallint", nullable=true)
     */
    private $estado;

    /**
     * @var integer
     *
     * @ORM\Column(name="id_vid", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $idVid;



    /**
     * Set nombre
     *
     * @param string $nombre
     * @return Vid
     */
    public function setNombre($nombre)
    {
        $this->nombre = $nombre;
   
        return $this;
    }

    /**
     * Get nombre
     *
     * @return string
     */
    public function getNombre()
    {
        return $this->nombre;
    }

    /**
     * Set descripcion
     *
     * @param string $descripcion
     * @return Vid
     */
    public function setDescripcion($descripcion)
    {
        $this->descripcion = $descripcion;
   
        return $this;
    }

    /**
     * Get descripcion
     *
     * @return string
     */
    public function getDescripcion()
    {
        return $this->descripcion;
    }

    /**
     * Set tipo
     *
     * @param smallint $tipo
     * @return Vid
     */
    public function setTipo($tipo)
    {
        $this->tipo = $tipo;
   
        return $this;
    }

    /**
     * Get tipo
     *
     * @return smallint
     */
    public function getTipo()
    {
        return $this->tipo;
    }

    /**
     * Set clasificacion
     *
     * @param smallint $clasificacion
     * @return Vid
     */
    public function setClasificacion($clasificacion)
    {
        $this->clasificacion = $clasificacion;
   
        return $this;
    }

    /**
     * Get clasificacion
     *
     * @return smallint
     */
    public function getClasificacion()
    {
        return $this->clasificacion;
    }

    /**
     * Set solicitaTiempo
     *
     * @param smallint $solicitaTiempo
     * @return Vid
     */
    public function setSolicitaTiempo($solicitaTiempo)
    {
        $this->solicitaTiempo = $solicitaTiempo;
   
        return $this;
    }

    /**
     * Get solicitaTiempo
     *
     * @return smallint
     */
    public function getSolicitaTiempo()
    {
        return $this->solicitaTiempo;
    }

    /**
     * Set estado
     *
     * @param smallint $estado
     * @return Vid
     */
    public function setEstado($estado)
    {
        $this->estado = $estado;
   
        return $this;
    }

    /**
     * Get estado
     *
     * @return smallint
     */
    public function getEstado()
    {
        return $this->estado;
    }

    /**
     * Get idVid
     *
     * @return integer
     */
    public function getIdVid()
    {
        return $this->idVid;
    }
}


Como se puede ver, tienen una relación entre ellas ManyToOne. Ahora lo que intento es persistir un objeto del tipo VIDGrupoSegmento de la siguiente manera:


//El objeto que le paso se trata de un objeto del tipo Vid
public function crearObjGrupoSegmentoPorDefecto($objetoVid) {
       
        $registroGS = new VidGrupoSegmento ();
        $registroGS->setIdVid ( $objetoVid);
        $registroGS->setEstado ( 1 );
        $this->em->persist ( $registroGS );
        $this->em->flush ();
       
        return $registroGS;
    }



Al hacerlo de la manera que indico, me salta un error de este tipo:

A new entity was found through the relationship 'RM\DiscretasBundle\Entity\VidGrupoSegmento#idVid' that was not configured to cascade persist operations for entity: RM\DiscretasBundle\Entity\Vid@000000006864251900000000561f1f3a. 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 'RM\DiscretasBundle\Entity\Vid#__toString()' to get a clue.


Investigando por ahí, le he puesto el cascade persist en la clase VidGrupoSegmento en la variable idVid, y ya no salta el error anterior, pero esto provoca que cada vez que persisto un objeto de este tipo, me persista de nuevo el objetoVid también y me lo cree de nuevo en la BD. Me gustaría poder persistir un objeto del tipo VidGrupoSegmento sin que se guarde de nuevo, ¿sabéis cómo puedo configurar esto?

Muchas gracias de antemano.

Ivan Javier Barranco Gavilan

unread,
Jul 8, 2015, 7:24:08 AM7/8/15
to symfony_...@googlegroups.com

Hola que tal, yo estoy con el mismo problema, conseguiste solucionarlo? Me podrias explicar como? Gracias!
Reply all
Reply to author
Forward
0 new messages