Ver Mensaje Individual
  #4 (permalink)  
Antiguo 31/07/2013, 11:25
daymerrf
 
Fecha de Ingreso: febrero-2013
Mensajes: 66
Antigüedad: 11 años, 2 meses
Puntos: 0
Respuesta: Dos entidades en un formulario

Ya me he leído la documentación que me recomendaste, pero tengo aun algunos problemas, el primero es que las validaciones de la entidad imagen no están funcionando ya que no validan y la segunda es que me está guardando en la base de datos el nombre de las imágenes sin la extensión…. A continuación muestro el código
Gracias y espero que me puedan seguir ayudando

Código PHP:
Ver original
  1. class Imagen
  2. {
  3.      /**
  4.      * @var string
  5.      *
  6.      * @ORM\Column(name="imagen", type="string", length=20, nullable=false)
  7.      * @Assert\File(maxSize = "500k", mimeTypes = {"application/jpg"})
  8.      */
  9.     protected $imagen;
  10.    
  11.     public function subirImagen($directorio)
  12.     {
  13.         if($this->imagen === null)
  14.             return false;
  15.         $nombreImagen = uniqid('anuncio-').'.'.$this->imagen->guessExtension();
  16.         $this->imagen->move($directorio, $nombreImagen);
  17.         $this->setImagen($nombreImagen);
  18.         return true;
  19.     }  
  20. }  
  21. class ImagenType extends AbstractType
  22. {
  23.     public function buildForm(FormBuilderInterface $builder, array $options)
  24.     {
  25.         $builder
  26.             ->add('imagen', 'file', array(
  27.                 'required' => false,
  28.                 'by_reference' => false,
  29.             ))
  30.         ;
  31.     }
  32.  
  33.     public function setDefaultOptions(OptionsResolverInterface $resolver)
  34.     {
  35.         $resolver->setDefaults(array(
  36.             'data_class' => 'Anuncios\FrontendBundle\Entity\Imagen'
  37.         ));
  38.     }
  39.  
  40.     public function getName()
  41.     {
  42.         return 'anuncios_frontendbundle_imagentype';
  43.     }
  44. }
  45. class AnuncioType extends AbstractType
  46. {
  47.     public function buildForm(FormBuilderInterface $builder, array $options)
  48.     {
  49.         $builder
  50.             ->add('asunto')
  51.             ->add('precio')
  52.             ->add('moneda')
  53.             ->add('descripcion')
  54.             ->add('nombre')
  55.             ->add('telefono')
  56.             ->add('categoria')
  57.             ->add('imagenes', 'collection', array(
  58.                 'type' => new ImagenType(),
  59.                 'by_reference' => false,
  60.                 ))
  61.         ;
  62.     }
  63.  
  64.     public function setDefaultOptions(OptionsResolverInterface $resolver)
  65.     {
  66.         $resolver->setDefaults(array(
  67.             'data_class' => 'Anuncios\FrontendBundle\Entity\Anuncio'
  68.         ));
  69.     }
  70.  
  71.     public function getName()
  72.     {
  73.         return 'anuncios_frontendbundle_anunciotype';
  74.     }
  75. }
  76. En el controller
  77.  public function anuncioNuevoAction()
  78.     {  
  79.         $anuncio = new Anuncio();
  80.         $imagen1 = new Imagen();
  81.         $imagen2 = new Imagen();
  82.         $imagen3 = new Imagen();
  83.         $anuncio->addImagene($imagen1);
  84.         $anuncio->addImagene($imagen2);
  85.         $anuncio->addImagene($imagen3);
  86.         $formulario = $this->createForm(new AnuncioType(), $anuncio);
  87.        
  88.         $peticion = $this->getRequest();
  89.         if($peticion->getMethod()== 'POST')
  90.         {
  91.            $formulario->bind($peticion);          
  92.            if($formulario->isValid())
  93.            {                  
  94.                $em = $this->getDoctrine()->getManager();
  95.                $anuncio->setFecha(new \DateTime());
  96.                $anuncio->setUsuario($this->getUser());
  97.                $directorio = $this->container->getParameter('anuncios.directorio.web');
  98.                if(!$imagen1->subirImagen($directorio))
  99.                     $anuncio->removeImagene($imagen1);              
  100.                if(!$imagen2->subirImagen($directorio))
  101.                    $anuncio->removeImagene($imagen2);
  102.                if(!$imagen3->subirImagen($directorio))
  103.                    $anuncio->removeImagene($imagen3);              
  104.                $em->persist($anuncio);
  105.                $em->flush();                
  106.            }
  107.         }
  108.         return $this->render('FrontendBundle:Anuncios:anuncioFormulario.html.twig', array(
  109.             'formulario' => $formulario->createView(),
  110.         ));
  111.     }