Symfony 2.2形式没有绑定请求

时间:2013-06-06 20:00:45

标签: php symfony doctrine-orm formcollection

我的控制器代码是

public function checkoutAction(Request $request) {
        $securityContext = $this->container->get('security.context');
        $request = $this->getRequest();
        $form = $this->createForm(new RequestRunnerType() );
        $data = array();

        if ($request->getMethod() == 'POST') {
            $orderService = $this->container->get('order_service');
            $paymentMethod = $request->request->get('paymentMethod');
            if ($paymentMethod == 'requsetRunner') {
                $form->bind($request->request->get('requestRunner'));
                if ($form->isValid()) {
                    //problem is here
                    $address = $form->getData();
                    $return = $orderService->createOrder($paymentMethod, $address['requestRunner']);
                }
            }
        }
    }

表单类代码是

   public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
                ->add('id', 'hidden')
                ->add('buildingNo', 'text',array(
                    'mapped' => false
                ))
                ->add('floorNo', 'text',array(
                    'mapped' => false
                ))
                ->add('street', 'text',array(
                    'mapped' => false
                ))
                ->add('city', 'text')
                ->add('landmark', 'text',array(
                    'mapped' => false
                ))
                ->add('apartmentNo', 'text',array(
                    'mapped' => false
                ))
                ->add('name', 'text')
                ->add('phoneNo', 'text')
                ->add('notes', 'textarea')
                ->add('currency', 'choice', array(
                    'choices' => array(
                        'EGP' => 'EGP',
                        'USD' => 'USD'
                    ))
        );
    }

我的实体

<?php

namespace myapp\bundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * RequestRunner
 *
 * @ORM\Table(name="request_runner")
 * @ORM\Entity
 */
class RequestRunner
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(name="orderId", type="integer", nullable=false)
     */
    private $orderid;

    /**
     * @var integer
     *
     * @ORM\Column(name="customerId", type="integer", nullable=false)
     */
    private $customerid;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, nullable=false)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="phoneNo", type="string", length=45, nullable=true)
     */
    private $phoneno;

    /**
     * @var string
     *
     * @ORM\Column(name="mobileNo", type="string", length=45, nullable=false)
     */
    private $mobileno;

    /**
     * @var string
     *
     * @ORM\Column(name="address", type="string", length=255, nullable=false)
     */
    private $address;

    /**
     * @var string
     *
     * @ORM\Column(name="city", type="string", length=255, nullable=true)
     */
    private $city;

    /**
     * @var string
     *
     * @ORM\Column(name="notes", type="text", nullable=true)
     */
    private $notes;

    /**
     * @var string
     *
     * @ORM\Column(name="comments", type="text", nullable=true)
     */
    private $comments;

    /**
     * @var float
     *
     * @ORM\Column(name="weight", type="decimal", nullable=true)
     */
    private $weight;

    /**
     * @var string
     *
     * @ORM\Column(name="appointment_from", type="string", length=255, nullable=false)
     */
    private $appointmentFrom;

    /**
     * @var string
     *
     * @ORM\Column(name="appointment_to", type="string", length=255, nullable=true)
     */
    private $appointmentTo;

    /**
     * @var float
     *
     * @ORM\Column(name="amount", type="decimal", nullable=false)
     */
    private $amount;

    /**
     * @var float
     *
     * @ORM\Column(name="deliveryFees", type="decimal", nullable=false)
     */
    private $deliveryfees;

    /**
     * @var string
     *
     * @ORM\Column(name="type", type="string", length=255, nullable=false)
     */
    private $type;

    /**
     * @var string
     *
     * @ORM\Column(name="employee", type="string", length=255, nullable=false)
     */
    private $employee;

    /**
     * @var string
     *
     * @ORM\Column(name="status", type="string", length=255, nullable=false)
     */
    private $status;

    /**
     * @var string
     *
     * @ORM\Column(name="courier", type="string", length=255, nullable=false)
     */
    private $courier;

    /**
     * @var integer
     *
     * @ORM\Column(name="time", type="integer", nullable=false)
     */
    private $time;

    /**
     * @var string
     *
     * @ORM\Column(name="waybill", type="string", length=255, nullable=true)
     */
    private $waybill='';

    /**
     * @var string
     *
     * @ORM\Column(name="urlWaybill", type="string", length=255, nullable=true)
     */
    private $urlwaybill;

    /**
     * @var float
     *
     * @ORM\Column(name="rate", type="decimal", nullable=false)
     */
    private $rate;

    /**
     * @var string
     *
     * @ORM\Column(name="currency", type="string", length=45, nullable=false)
     */
    private $currency;

    /**
     * @var string
     *
     * @ORM\Column(name="deliveryTime", type="string", length=255, nullable=false)
     */
    private $deliverytime=0;

    /**
     * @var string
     ***
     * @ORM\Column(name="chequeNo", type="string", length=45, nullable=false)
     */
    private $chequeno=0;
}

我的问题:** 当我将请求绑定到表单时,我得到的对象充满了NULL 当我打印$request->request->all();时 我在post请求中得到的值如下所示

array (size=2)
  'paymentMethod' => string 'requsetRunner' (length=13)
  'requestRunner' => 
    array (size=12)
      'city' => string 'Cairo' (length=5)
      'buildingNo' => string '' (length=0)
      'street' => string '38 Hassnin Abdel Kader, Nasr City' (length=33)
      'apartmentNo' => string '' (length=0)
      'floorNo' => string '0' (length=1)
      'id' => string '' (length=0)
      'name' => string '' (length=0)
      'phoneNo' => string '' (length=0)
      '_token' => string '1a8d21d49a5eb612a8b863b792b89b0ed0bb8b67' (length=40)
      'currency' => string 'EGP' (length=3)
      'landmark' => string 'Next to supermarket' (length=19)
      'notes' => string '' (length=0)

我试过

$form->bind($request->request->get('requestRunner'));

$form->bind($request));

因为Symfony 2.2应该获取数组并获取文档所说的形式,但两者都不起作用

1 个答案:

答案 0 :(得分:1)

我想我知道这是什么问题。

没有任何内容可以绑定到请求。

我的意思是没有要映射到的requestRunner对象。您有两个选项,无论是在表单创建中,是通过它还是稍后使用$form->setData($requestRunner)

我没有对它进行测试,但这应该可行:

public function checkoutAction(Request $request) {
        $securityContext = $this->container->get('security.context');
        $obj  = new RequestRunner();
        $form = $this->createForm(new RequestRunnerType(), $obj );
        $data = array();

        if ($request->getMethod() == 'POST') {
            $orderService = $this->container->get('order_service');
            $paymentMethod = $request->get('paymentMethod');
            if ($paymentMethod == 'requsetRunner') {
                $form->bind($request->get('requestRunner'));
                if ($form->isValid()) {
                    //problem is here
                    //and here you can use the $obj variable what will be filled with the request data
                    $return = $orderService->createOrder($paymentMethod, $obj);
                }
            }
        }
    }

如果我正确地获得了您的代码,您想要将整个requestRunner传递给createOrder函数吗?

我也更改了一些位,你不需要getRequest()因为你在函数的参数列表中有这个,如果你从请求中提取数据,你可以使用get函数请求,或$this->getRequest()->get('input name')

如果不能像这样工作,那么从createForm和be bind中删除$ obj,使用$form->setData($obj)功能。 (抱歉不记得哪一个是2.2,哪一个是2.3方式)