0

我的控制器代码是

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 应该获取的数组并按照文档所述获取表单,但两者都不起作用

4

1 回答 1

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 和之前删除 $obj bind,使用该$form->setData($obj)功能。(抱歉不记得哪个是2.2,哪个是2.3方式)

于 2013-06-07T23:42:07.417 回答