【问题标题】:save forms of relationed tables symfony2 using entityType class使用 entityType 类保存关系表 symfony2 的形式
【发布时间】:2017-03-17 04:15:57
【问题描述】:

我再次需要 symfony2 的帮助。我有两个类ProductosCategoria,当我在Productos 的视图列表中显示categoriaId 字段时,该值是一个整数...我使用entityType 字段来显示我的类@ 的NombreCategoria 987654326@...但是当我保存Productos的形式时,我的BD中categoriaId字段的值为0...我不知道问题出在哪里..我想将id保存在我的班级Producto.. 这是我的看法

<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\Mapping 作为 ORM;

/** * 类别 */ 班级类别 { /** * @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;
}

}

【问题讨论】:

  • 您的两个实体之间没有映射?? (多对一,多对多?...)。您的类别实体的代码在哪里?
  • 如果您使用实体表单类型,表单组件会使用实体对象(类别)调用 Productos 的 setCategoriaId - 不是该实体的 id。因此,使用 categoriaId 的表单配置,您的私有 $categoriaId 在提交后将是一个对象,而不是整数。也许您的数据库层正在将之后的对象转换为值“0”?

标签: php forms symfony doctrine-orm


【解决方案1】:

问题实际上是一个实体关系 oneToMany 和 manyToOne...

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: {  }

一切都好...谢谢大家..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 2011-10-26
    • 1970-01-01
    相关资源
    最近更新 更多