Foros del Web » Programando para Internet » PHP » Symfony »

[SOLUCIONADO] ArrayCollection vacio.

Estas en el tema de ArrayCollection vacio. en el foro de Symfony en Foros del Web. muy buenas. solicito ayuda para intentar resolver un problema que llevo como 6 horas intentando resolver. Estoy realizando un formulario embebido 1:n. y estoy en ...
  #1 (permalink)  
Antiguo 24/04/2013, 16:54
 
Fecha de Ingreso: abril-2013
Mensajes: 12
Antigüedad: 11 años
Puntos: 1
ArrayCollection vacio.

muy buenas.
solicito ayuda para intentar resolver un problema que llevo como 6 horas intentando resolver.
Estoy realizando un formulario embebido 1:n. y estoy en el siguiente estado:

En la web me sale bien el formulario principal y puedo agregar y eliminar los formularios embebidos perfectamente.
Ahora bien, en el momento de de guardarlo todo, me guarda el principal pero los embebidos no.
el codigo del controlador createAction es el siguiente:

Código PHP:
$entity  = new Estudio();
        
$form $this->createForm(new EstudioType(), $entity);
        
$form->bind($request);

        if (
$form->isValid()) {
            
$em $this->getDoctrine()->getManager();
            
$em->persist($entity);
            
$em->flush();
            foreach(
$entity->getPartesEstudio() as $parte){
                
$parte->setIdEstudio($entity);
                
$em->persist($parte);//  
            
}
            
$em->flush(); 
He comprobado que dentro del foreach no entra y que $entity->getPartesEstudio() no devuelve null. pero ya no sque más probar.

La clase estudio es:

Código PHP:
<?php

namespace proyectobackendBundleEntity
;
use 
DoctrineCommonCollectionsArrayCollection;

use 
DoctrineORMMapping as ORM;

/**
 * Estudio
 */
class Estudio
{
    
/**
     * @var integer
     */
    
private $id;

    
/**
     * @var string
     */
    
private $nombre;

    
/**
     * @var integer
     */
    
private $numPartes;

    
/**
     * @var integer
     */
    
private $numDatosEstudio;

    
/**
     * @var string
     */
    
private $descripcion;

    
/**
     * @var boolean
     */
    
private $activa 0;

    protected 
$partesEstudio;

    
    public function 
__construct()
    {
        
$this->partesEstudio = new ArrayCollection();
    }
    
/**
     * Get id
     *
     * @return integer 
     */
    
public function getId()
    {
        return 
$this->id;
    }

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

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

    
/**
     * Set numPartes
     *
     * @param integer $numPartes
     * @return Estudio
     */
    
public function setNumPartes($numPartes)
    {
        
$this->numPartes $numPartes;
    
        return 
$this;
    }

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

    
/**
     * Set numDatosEstudio
     *
     * @param integer $numDatosEstudio
     * @return Estudio
     */
    
public function setNumDatosEstudio($numDatosEstudio)
    {
        
$this->numDatosEstudio $numDatosEstudio;
    
        return 
$this;
    }

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

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

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

    
/**
     * Set activa
     *
     * @param boolean $activa
     * @return Estudio
     */
    
public function setActiva($activa)
    {
        
$this->activa $activa;
    
        return 
$this;
    }

    
/**
     * Get activa
     *
     * @return boolean 
     */
    
public function getActiva()
    {
        return 
$this->activa;
    }
        
     public function 
setPartesEstudio(ArrayCollection  $partesEstudio null)
    {
        
$this->partesEstudio $partesEstudio;
    }
    public function 
getPartesEstudio()
    {
        return 
$this->partesEstudio;
    }
    public function 
__toString (){
        return 
$this->nombre;
    }
}
y la clase parteEstudio es:

Código PHP:
<?php

namespace proyectobackendBundleEntity
;

use 
DoctrineORMMapping as ORM;

/**
 * ParteEstudio
 */
class ParteEstudio
{
    
/**
     * @var integer
     */
    
private $id;

    
/**
     * @var string
     */
    
private $nombre;

    
/**
     * @var integer
     */
    
private $numOrden;

    
/**
     * @var integer
     */
    
private $numPreguntas;

    
/**
     * @var string
     */
    
private $titulo;

    
/**
     * @var boolean
     */
    
private $guardarTiempoRespuesta 0;

    
/**
     * @var string
     */
    
private $tipoTratamientoDatos;

    
/**
     * @var integer
     */
    
private $numPreguntasDePrueba;

    
/**
     * @var \proyecto\backendBundle\Entity\Estudio
     */
    
private $idEstudio;

    
/**
     * @var \proyecto\backendBundle\Entity\TipoPregunta
     */
    
private $idTipoPregunta;


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

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

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

    
/**
     * Set numOrden
     *
     * @param integer $numOrden
     * @return ParteEstudio
     */
    
public function setNumOrden($numOrden)
    {
        
$this->numOrden $numOrden;
    
        return 
$this;
    }

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

    
/**
     * Set numPreguntas
     *
     * @param integer $numPreguntas
     * @return ParteEstudio
     */
    
public function setNumPreguntas($numPreguntas)
    {
        
$this->numPreguntas $numPreguntas;
    
        return 
$this;
    }

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

    
/**
     * Set titulo
     *
     * @param string $titulo
     * @return ParteEstudio
     */
    
public function setTitulo($titulo)
    {
        
$this->titulo $titulo;
    
        return 
$this;
    }

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

    
/**
     * Set guardarTiempoRespuesta
     *
     * @param boolean $guardarTiempoRespuesta
     * @return ParteEstudio
     */
    
public function setGuardarTiempoRespuesta($guardarTiempoRespuesta)
    {
        
$this->guardarTiempoRespuesta $guardarTiempoRespuesta;
    
        return 
$this;
    }

    
/**
     * Get guardarTiempoRespuesta
     *
     * @return boolean 
     */
    
public function getGuardarTiempoRespuesta()
    {
        return 
$this->guardarTiempoRespuesta;
    }

    
/**
     * Set tipoTratamientoDatos
     *
     * @param string $tipoTratamientoDatos
     * @return ParteEstudio
     */
    
public function setTipoTratamientoDatos($tipoTratamientoDatos)
    {
        
$this->tipoTratamientoDatos $tipoTratamientoDatos;
    
        return 
$this;
    }

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

    
/**
     * Set numPreguntasDePrueba
     *
     * @param integer $numPreguntasDePrueba
     * @return ParteEstudio
     */
    
public function setNumPreguntasDePrueba($numPreguntasDePrueba)
    {
        
$this->numPreguntasDePrueba $numPreguntasDePrueba;
    
        return 
$this;
    }

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

    
/**
     * Set idEstudio
     *
     * @param \proyecto\backendBundle\Entity\Estudio $idEstudio
     * @return ParteEstudio
     */
    
public function setIdEstudio(proyectobackendBundleEntityEstudio $idEstudio null)
    {
        
$this->idEstudio $idEstudio;
    
        return 
$this;
    }

    
/**
     * Get idEstudio
     *
     * @return \proyecto\backendBundle\Entity\Estudio 
     */
    
public function getIdEstudio()
    {
        return 
$this->idEstudio;
    }

    
/**
     * Set idTipoPregunta
     *
     * @param \proyecto\backendBundle\Entity\TipoPregunta $idTipoPregunta
     * @return ParteEstudio
     */
    
public function setIdTipoPregunta(proyectobackendBundleEntityTipoPregunta $idTipoPregunta null)
    {
        
$this->idTipoPregunta $idTipoPregunta;
    
        return 
$this;
    }

    
/**
     * Get idTipoPregunta
     *
     * @return \proyecto\backendBundle\Entity\TipoPregunta 
     */
    
public function getIdTipoPregunta()
    {
        return 
$this->idTipoPregunta;
    }
}
Gracias de antemano y un saludo!!
  #2 (permalink)  
Antiguo 24/04/2013, 17:51
Avatar de masterpuppet
Software Craftsman
 
Fecha de Ingreso: enero-2008
Ubicación: Montevideo, Uruguay
Mensajes: 3.550
Antigüedad: 16 años, 3 meses
Puntos: 845
Respuesta: ArrayCollection vacio.

Postea el mapeo y comprueba que tengas seteado correctamente el cascade.

Saludos.
__________________
http://es.phptherightway.com/
thats us riders :)
  #3 (permalink)  
Antiguo 25/04/2013, 14:01
 
Fecha de Ingreso: abril-2013
Mensajes: 12
Antigüedad: 11 años
Puntos: 1
Respuesta: ArrayCollection vacio.

esto es estudio:
Código:
proyecto\backendBundle\Entity\Estudio:
    type: entity
    table: estudio
    fields:
        id:
            id: true
            type: integer
            unsigned: false
            nullable: false
            generator:
                strategy: IDENTITY
        nombre:
            type: string
            length: 255
            fixed: false
            nullable: false
        numPartes:
            type: integer
            unsigned: false
            nullable: false
            column: num_partes
        numDatosEstudio:
            type: integer
            unsigned: false
            nullable: false
            column: num_datos_estudio
        descripcion:
            type: string
            length: 255
            fixed: false
            nullable: false
        activa:
            type: boolean
            nullable: false
    oneToMany:
        partesEstudio:
            targetEntity: ParteEstudio
            mappedBy: idEstudio
            cascade: [persist]
    lifecycleCallbacks: {  }
y esto es partesEstudio:

Código:
proyecto\backendBundle\Entity\ParteEstudio:
    type: entity
    table: parte_estudio
    fields:
        id:
            id: true
            type: integer
            unsigned: false
            nullable: false
            generator:
                strategy: IDENTITY
        nombre:
            type: string
            length: 255
            fixed: false
            nullable: false
        numOrden:
            type: integer
            unsigned: false
            nullable: false
            column: num_orden
        numPreguntas:
            type: integer
            unsigned: false
            nullable: false
            column: num_preguntas
        titulo:
            type: string
            length: 255
            fixed: false
            nullable: true
        guardarTiempoRespuesta:
            type: boolean
            nullable: true
            column: guardar_tiempo_respuesta
        tipoTratamientoDatos:
            type: string
            length: 255
            fixed: false
            nullable: true
            column: tipo_tratamiento_datos
        numPreguntasDePrueba:
            type: integer
            unsigned: false
            nullable: false
            column: num_preguntas_de_prueba
    manyToOne:
        idEstudio:
            targetEntity: Estudio
            cascade: {  }
            mappedBy: null
            inversedBy: PartesEstudio
            joinColumns:
                id_estudio:
                    referencedColumnName: id
            orphanRemoval: false
        idTipoPregunta:
            targetEntity: TipoPregunta
            cascade: {  }
            mappedBy: null
            inversedBy: null
            joinColumns:
                id_tipo_pregunta:
                    referencedColumnName: id
            orphanRemoval: false
    lifecycleCallbacks: {  }

¿con lo del seteado del cascade a que te refieres?

Un saludo.
  #4 (permalink)  
Antiguo 26/04/2013, 14:56
 
Fecha de Ingreso: abril-2013
Mensajes: 12
Antigüedad: 11 años
Puntos: 1
Respuesta: ArrayCollection vacio.

alguna idea?
  #5 (permalink)  
Antiguo 27/04/2013, 10:16
Avatar de fhederico  
Fecha de Ingreso: agosto-2009
Mensajes: 247
Antigüedad: 14 años, 8 meses
Puntos: 23
Respuesta: ArrayCollection vacio.

Hola,

Sobre esto:

Código HTML:
Ver original
  1. if ($form->isValid()) {
  2.             $em = $this->getDoctrine()->getManager();
  3.             $em->persist($entity);
  4.             $em->flush();
  5.             foreach($entity->getPartesEstudio() as $parte){
  6.                 $parte->setIdEstudio($entity);
  7.                 $em->persist($parte);//  
  8.             }
  9.             $em->flush();


Yo lo deje asi, porque tambien tenia un problema similar


Código HTML:
Ver original
  1. foreach ($entity->getPartesEstudio() as $parte)
  2.                 {
  3.                     $addParte = new ParteEstudio();
  4.                     $addParte ->setIdEstudio($parte);
  5.                     $em->persist($addParte );
  6.                 }
  7.  
  8.                 $em->flush();

Hace lo mismo que el tuyo, con la diferencia que crea la Instancia nueva antes de setear.

Lo otro, no entiendo porque haces dos "$em->flush();", si cada vez que ejecutas el "$em->persist(...)", esto va quedando en memoria, puedes ejecutar varios y al final cuando ejecutas el "flush" cargara todas las consultas que se hayan generado con persist.

Por ultimo, te recomiendo que valides que tu formulario se haya enviado por ejemplo por Post

Código HTML:
Ver original
  1. $request = $this->getRequest();    
  2.        
  3.         //Si se envio, verificamos, si no, seteamos un formulario nuevo
  4.         if($request->getMethod() === 'POST')
  5.                   {}


A y lo de "cascade" eso va en la entidad, es para que se actualicen en cascada.

Saludos
  #6 (permalink)  
Antiguo 27/04/2013, 11:28
Avatar de masterpuppet
Software Craftsman
 
Fecha de Ingreso: enero-2008
Ubicación: Montevideo, Uruguay
Mensajes: 3.550
Antigüedad: 16 años, 3 meses
Puntos: 845
Respuesta: ArrayCollection vacio.

Te dejo la referencia a(cascade operations), en tu caso es correcto, es el cascade[persist] que tienes en el mapeo y recuerda que doctrine solo comprueba el owning side(esto es seguramente lo que te esta faltando).

Saludos.
__________________
http://es.phptherightway.com/
thats us riders :)
  #7 (permalink)  
Antiguo 11/06/2013, 04:01
 
Fecha de Ingreso: abril-2013
Mensajes: 12
Antigüedad: 11 años
Puntos: 1
Respuesta: ArrayCollection vacio.

Hola.
Gracias por la ayuda. ya lo solucioné.

Un saludo.

Etiquetas: embebido, form
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 18:49.