Symfony \ Component \ Form \ Form :: handleRequest性能引起了人们的注意

时间:2016-06-01 21:28:40

标签: php forms symfony-forms symfony

我的问题是,为什么handleRequest方法与请求中的其他操作相比速度过慢? HandleRequest几乎与呈现模板一样长。我的Entity中没有涉及数据库或Doctrine ORM,它只是一个普通的PHP类,用于将用户数据绑定到表单。我已经对实体进行了一些合理的验证,但即使删除验证注释也几乎没有什么可以提高此方法的性能。

我正在使用Symfony 3.0.6和PHP 7.0.7

enter image description here

控制器

public function postSearchAction(Request $request, $page = null)
{
    $this->setRequestPage($request, $page);
    $formData = new SearchRequest();
    $this->startTimeline("building form");
    $searchRequestForm = $this->createForm(SearchType::class, $formData, array(
        'action' => $this->generateUrl('api_search_post'),
        'method' => 'POST',
    ));
    $this->stopTimeline("building form");

    $this->startTimeline("handle request");
    $searchRequestForm->handleRequest($request);
    $this->stopTimeline("handle request");

    if ($searchRequestForm->isValid()) {
        $formData = $searchRequestForm->getData();
        $this->startTimeline("build service request");
        $searchRequest = $this->get('search')->buildRequest($formData);
        $this->stopTimeline("build service request");

        if ($formData->getSearchType() === SearchRequest::TYPE_VEHICLE) {
            $search = $this->get('search')->getVehicle($searchRequest, $this->getStopwatch());
        } else {
            $search = $this->get('search')->getStorage($searchRequest, $this->getStopwatch());
        }
    } else {
        $search = $searchRequestForm;
    }

    $view = $this->view($search, 200)
        ->setSerializationContext($this->getSerializationContext())
        ->setTemplate("RestApiBundle:Search:postSearch.html.twig")
        ->setTemplateVar('search')
        ->setTemplateData(array(
            'form' => $searchRequestForm->createView()
        ));

    return $this->handleView($view);
}

表格

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('location', TextType::class)
        ->add('page', HiddenType::class, ['required' => false])
        ->add('searchType', ChoiceType::class, [
            'required' => true,
            'label' => 'Search Type',
            'choices' =>[
                'Storage' => SearchRequest::TYPE_STORAGE,
                'Vehicle' => SearchRequest::TYPE_VEHICLE
            ]])
        ->add('squareFoot', ChoiceType::class, [
            'required' => false,
            'label' => 'Unit Size',
            'placeholder' => 'Unit Size',
            'choices' => [
                '5\' x 5\''   => 25,
                '5\' x 10\''  => 50,
                '5\' x 15\''  => 75,
                '10\' x 10\'' => 100,
                '10\' x 15\'' => 150,
                '10\' x 20\'' => 200,
                '10\' x 25\'' => 250,
                '10\' x 30\'' => 300,
                'Default' => 0
            ]])
        ->add('sort', ChoiceType::class, [
            'required' => false,
            'label' => 'Sort By',
            'placeholder' => 'Sort By',
            'choices' => [
                'Distance'  => SearchRequest::SORT_DISTANCE,
                'Price'     => SearchRequest::SORT_PRICE,
                'Rating'    => SearchRequest::SORT_RATINGS,
                'Default'   => SearchRequest::SORT_DEFAULT
            ]])
        ->add('moveInDate', DateType::class, [
            'widget' => 'single_text',
            'required' => false
        ])
        ->add('filterClimateControlled', CheckboxType::class, [
            'value' => 'true',
            'required' => false,
            'label' => 'Climate Controlled'
        ])
        ->add('filter24HourAccess', CheckboxType::class, [
            'value' => 'true',
            'required' => false,
            'label' => '24 Hour Access'
        ])
        ->add('filterDriveUpAccess', CheckboxType::class, [
            'value' => 'true',
            'required' => false,
            'label' => 'Drive Up Access'
        ])
        ->add('search', SubmitType::class);
}

实体

class SearchRequest
{

const SORT_DISTANCE = 'distance';
const SORT_PRICE = 'price';
const SORT_RATINGS = 'ratings';
const SORT_DEFAULT = 'default';

const TYPE_VEHICLE = 'vehicle';
const TYPE_STORAGE = 'storage';

/**
 * @var string
 * @Assert\NotBlank()
 */
protected $location;

/**
 * @var int
 * @Assert\Type(
 *     type="numeric"
 * )
 */
protected $page = 1;

/**
 * @var double
 * @Assert\Type(
 *     type="numeric"
 * )
 */
protected $squareFoot;

/**
 * @var string
 * @Assert\Expression(
 *     "this.getSort() in this.getSortList()",
 *     message="Invalid sort type."
 * )
 */
protected $sort;

/**
 * @var string
 * @Assert\Expression(
 *     "this.getSearchType() in this.getSearchTypeList()",
 *     message="Invalid search type."
 * )
 */
protected $searchType;

/**
 * @var string
 * @Assert\Date()
 * @Serializer\Type("DateTime")
 */
protected $moveInDate;

/**
 * @var boolean
 * @Assert\Type(
 *     type="bool"
 * )
 */
protected $filterClimateControlled;

/**
 * @var boolean
 * @Assert\Type(
 *     type="bool"
 * )
 */
protected $filter24HourAccess;

/**
 * @var boolean
 * @Assert\Type(
 *     type="bool"
 * )
 */
protected $filterDriveUpAccess;


public function getSortList()
{
    return [
        null,
        self::SORT_DISTANCE,
        self::SORT_PRICE,
        self::SORT_RATINGS,
        self::SORT_DEFAULT
    ];
}

public function getSearchTypeList()
{
    return [
        self::TYPE_VEHICLE,
        self::TYPE_STORAGE
    ];
}

0 个答案:

没有答案