2017-03-17 28 views
0

我需要symfony2再次帮助。我有2个类ProductosCategoria,当我在我的视图列表中显示Productos在字段categoriaId的值是一个整数...我用entityType字段显示我的类Categoria ...但是当我保存的形式Productos,我categoriaId字段的值是在我的BD 0 ...我不知道哪里出了问题..我想保存在我的Productoid .. 这是我的看法使用entityType类保存关联表symfony2的形式

<form role="form" method="POST" enctype="multipart/form-data" action="{{path('producto_new')}}"> 
    <table class="record_properties"> 
     {{form(form)}} 
    </table> 
    <table> 
     <tr> 
      <td><button type="submit" class="btn btn-primary">Enviar</button></td> 
     </tr> 
    </table> 
</form> 

这是我的实体

namespace ebay\bdBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

class producto{ 
    private $id; 
    private $nombre; 
    private $categoriaId; 
    private $descripcion; 
    private $color; 
    private $precio; 
    private $imag; 
    private $total; 
    private $flag; 

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

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

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

    /** 
    * Set categoriaId 
    * @param integer $categoriaId 
    * @return producto 
    */ 
    public function setCategoriaId($categoriaId){ 
     $this->categoriaId = $categoriaId; 

     return $this; 
    } 

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

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

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

    /** 
    * Set color 
    * @param string $color 
    * @return producto 
    */ 
    public function setColor($color){ 
     $this->color = $color; 
     return $this; 
    } 

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

    /** 
    * Set precio 
    * @param integer $precio 
    * @return producto 
    */ 
    public function setPrecio($precio){ 
     $this->precio = $precio; 
     return $this; 
    } 

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

    /** 
    * Set envio 
    * @param string $imag 
    * @return producto 
    */ 
    public function setImag($imag){ 
     $this->imag = $imag; 
     return $this; 
    } 

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

    /** 
    * Set total 
    * @param integer $total 
    * @return producto 
    */ 
    public function setTotal($total){ 
     $this->total = $total; 
     return $this; 
    } 

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

    /** 
    * Set flag 
    * @param boolean $flag 
    * @return producto 
    */ 
    public function setFlag($flag){ 
     $this->flag = $flag; 
     return $this; 
    } 
    /** 
    * Get flag 
    * @return boolean 
    */ 
    public function getFlag() 
    { return $this->flag; } 
} 

这是我的形式

public function buildForm(FormBuilderInterface $builder, array $options){ 
    $builder 
     ->add('nombre','text', array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'))) 
     ->add('categoriaId','entity', array('class'=>'bdBundle:categoria','label'=>'descripcion', 'attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'),'data' => '$id')) 
     ->add('descripcion','text', array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'))) 
     ->add('color','text', array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'))) 
     ->add('precio','text', array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'))) 
     ->add('imag','file', array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'),'label'=>'Imagen', 'data_class'=>null)) 
     ->add('total','text', array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'))) 
     ->add('flag','choice', array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'),'choices'=>array('1'=>'Vigente'),'required'=>true)) 
    ; 
} 

,这是我的控制器

public function newAction(Request $request){ 
    $entity = new Producto(); 
    $form = $this->createForm(new productoType, $entity); 
    if ($request->getMethod() == 'POST'){ 
     $form->handleRequest($request); 
     $image = $entity->getImag(); 

     if(($image instanceof UploadedFile) && ($image->getError() == '0')){ 
      $originalName = $image->getClientOriginalName(); 
      $name_array = explode('.',$originalName); 
      $file_type = $name_array[sizeof($name_array) - 1]; 
      $valid_filetypes = array('jpg','jpeg','png'); 
      if(in_array(strtolower($file_type), $valid_filetypes)){ 
       $document = new Document(); 
       $document->setFile($image); 

       $document->setSubDirectory('productos'); 
       $document->processFile(); 

       $entity->setImag($image->getBasename()); 

       $em = $this->getDoctrine()->getManager(); 

       $em->persist($entity); 
       $em->flush(); 

       $this->get('session')->getFlashBag()->add('mensaje', 
         'Se inserto la imagen correctamente'); 
       return $this->redirect($this->generateUrl('producto')); 
      }else{ 
       $this->get('session')->getFlashBag()->add('mensaje', 
         'La extension del archivo no es la correcta'); 
       return $this->redirect($this->generateUrl('producto_new')); 
      } 
     }else{ 
      $this->get('session')->getFlashBag()->add('mensaje', 
        'No ingreso el archivo o se produjo un error inesperado'); 
      return $this->redirect($this->generateUrl('producto_new')); 
     } 
    } 

    return $this->render('bdBundle:producto:new.html.twig', array(
     'form' => $form-> createView(),)); 
} 

任何人都可以帮我请..

我忘记展示我Categoria实体...

namespace ebay\bdBundle\Entity; 

使用Doctrine \ ORM \ Mappi作为ORM;

/** * categoria */ 类categoria { /** * @var整数 */ 私人的$ id;

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

/** 
* @var boolean 
*/ 
private $flag; 

public function __toString() 
{ 
    return $this->getDescripcion(); 
} 


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

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

    return $this; 
} 

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

/** 
* Set flag 
* 
* @param boolean $flag 
* @return categoria 
*/ 
public function setFlag($flag) 
{ 
    $this->flag = $flag; 

    return $this; 
} 

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

}

+0

你有你的两个实体之间没有映射? (ManyToOne,ManyToMany?...)。 Categoria实体的代码在哪里? –

+0

如果您使用的是实体表单类型,表单组件会使用实体对象(Categoria)调用Productos的setCategoriaId - * not *该实体的id。因此,对于categoriaId的表单配置,您的私有$ categoriaId将在提交后成为对象,而不是整数。 也许你的数据库层将对象转换为值“0”? –

回答

0

efectly问题是实体间关系一对多和多对一...

manyToOne: 
    proveedor: 
     targetEntity: Proveedor 
     cascade: { } 
     mappedBy: null 
     inversedBy: null 
     joinColumns: 
      proveedor_id: 
       referencedColumnName: id 
     orphanRemoval: false 
    producto: 
     targetEntity: Producto 
     cascade: { } 
     mappedBy: null 
     inversedBy: null 
     joinColumns: 
      producto_id: 
       referencedColumnName: id 
     orphanRemoval: false 
lifecycleCallbacks: { } 

与所有它的确定...感谢所有的家伙..