编辑表单返回空值Symfony2

时间:2016-09-15 20:16:45

标签: symfony

我的应用程序中有一个编辑表单,当我尝试保存(插入)新记录时,我收到此错误消息:

An exception occurred while executing 'INSERT INTO in_orden_compra_detalle (id_orden_compra, id_producto, cantidad, precio_compra, total, entrega, fecha_entrega) VALUES (?, ?, ?, ?, ?, ?, ?)' with params [null, null, 89, 45, null, null, null]:

表in_orden_compra_detalle与2个表(in_producto和in_orden_compra)相关

这是我的关系:

class InOrdenCompra
{
    /**
     * @ORM\ManyToOne(targetEntity="InProveedor", inversedBy="InOrdenCompra")
     * @ORM\JoinColumn(name="id_proveedor", referencedColumnName="id")
     */
    protected $proveedorOc;

    /**
     * @ORM\ManyToOne(targetEntity="Nival\AccesoBundle\Entity\AdUsuario", inversedBy="InOrdenCompra")
     * @ORM\JoinColumn(name="id_usuario", referencedColumnName="id_usuario")
     */
    /**
     * @ORM\OneToMany(targetEntity="InOrdenCompraDetalle", mappedBy="InOrdenCompra", cascade={"persist"})
     */
    protected $ordenCompra;



class InProducto
{

    /**
     * @ORM\OneToMany(targetEntity="InOrdenCompraDetalle", mappedBy="InProducto", cascade={"persist"})
     */
    protected $productoOc;

    public function __construct()
    {
        $this->productoOc = new ArrayCollection();
    }

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


class InOrdenCompraDetalle
{
    /**
     * @ORM\ManyToOne(targetEntity="InOrdenCompra", inversedBy="InOrdenCompraDetalle")
     * @ORM\JoinColumn(name="id_orden_compra", referencedColumnName="id")
     */
    protected $ordenCompra;

    /**
     * @ORM\ManyToOne(targetEntity="InProducto", inversedBy="InOrdenCompraDetalle")
     * @ORM\JoinColumn(name="id_producto", referencedColumnName="id_producto")
     */
    protected $productoOc;

我的新记录控制器:

public function newAction($id_orden_compra)
    {
        $entity = new InOrdenCompraDetalle();

        $entity->setOrdenCompra($this->get('doctrine')->getRepository('NivalInventarioBundle:InOrdenCompra')->find($id_orden_compra));

        $form   = $this->createCreateForm($entity, $id_orden_compra);

        return $this->render('NivalInventarioBundle:InOrdenCompraDetalle:new.html.twig', array(
            'id_orden_compra' => $id_orden_compra,
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

我需要将id_orden_compra值传递给实体InOrdenCompraDetalle中的setOrdenCompra。

此外,id_producto应具有从实体字段中选择的值:

->add('productoOc', 'entity', array(
            'class' => 'NivalInventarioBundle:InProducto',
            'query_builder' => function (EntityRepository $er) {
                return $er->createQueryBuilder('u')
                    ->orderBy('u.nombre', 'ASC');
            },
            'choice_label' => function ($displayname) {
                                return $displayname->getDisplayName();},
            'by_reference' => true,
            'expanded' => false,
            'placeholder' => 'Seleccione una opción',
            'mapped' => true,
            'multiple' => false,
        ))
        ->add('cantidad')
        ->add('precioCompra')

但也是空的。

请帮忙

已更新

public function createAction(Request $request, $id_orden_compra)
    {
        $entity = new InOrdenCompraDetalle();

        $form = $this->createCreateForm($entity, $id_orden_compra);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('inordencompradetalle_show', array('id_orden_compra' => $id_orden_compra, 'id' => $entity->getId())));
        }

        return $this->render('NivalInventarioBundle:InOrdenCompraDetalle:new.html.twig', array(
            'id_orden_compra' => $id_orden_compra,
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

createCreateForm:

private function createCreateForm(InOrdenCompraDetalle $entity, $id_orden_compra)
    {
        $form = $this->createForm(new InOrdenCompraDetalleType(), $entity, array(
            'action' => $this->generateUrl('inordencompradetalle_create', array('id_orden_compra' => $id_orden_compra)),
            'method' => 'POST',
        ));

        return $form;
    }

0 个答案:

没有答案