我有一个表单,它是我的 ajax 请求的瓶颈。
$order = $this->getDoctrine()
->getRepository('AcmeMyBundle:Order')
->find($id);
$order = $order ? $order : new Order();
$form = $this->createForm(new OrderType(), $order);
$formView = $form->createView();
return $this->render(
'AcmeMyBundle:Ajax:order_edit.html.twig',
array(
'form' => $formView,
)
);
为了更干净的代码我删除了stopwatch
声明。
My 订单类型有下一个字段:
$builder
->add('status') // enum (string)
->add('paid_status') // enum (string)
->add('purchases_price') // int
->add('discount_price') // int
->add('delivery_price') // int
->add('delivery_real_price', null, array('required' => false)) // int
->add('buyer_name') // string
->add('buyer_phone') // string
->add('buyer_email') // string
->add('buyer_address') // string
->add('comment') // string
->add('manager_comment') // string
->add('delivery_type') // enum (string)
->add('delivery_track_id') // string
->add('payment_method') // enum (string)
->add('payment_id') // string
->add('reward') // int
->add('reward_status') // enum (string)
->add('container') // string
->add('partner') // Entity: User
->add('website', 'website') // Entity: Website
->add('products', 'collection', array( // Entity: Purchase
'type' => 'purchase',
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'property_path' => 'purchases',
'error_bubbling' => false,
));
购买类型:
$builder
->add('amount')
->add('price')
->add('code', 'variant', array(
'property_path' => 'variantEntity',
'data_class' => '\Acme\MyBundle\Entity\Simpla\Variant'
))
;
Also 购买类型有一个听众在这里并不重要。它在 Symfony 分析器中表示如下variant_retrieve
, purchase_form_creating
。可以看到大约需要200ms。
Here I put the result of profilers:
如你看到的:$this->createForm(...)
需要 1011 毫秒,$form->createView();
需要 2876 毫秒,twig 中的表单渲染也非常慢:4335 毫秒。正如 blackfire profiler 所说,所有交易都在ObjectHydrator::gatherRowData()
and UnitOfWork::createEntity()
.
Method createEntity()
调用 2223 次,因为有一些字段映射为Variant
实体并具有表单类型Entity
。但从上面的代码可以看出,没有entity
变体的类型。我的VariantType
是简单的扩展text
表单类型具有modelTransformer
。为了不搞乱一切,您可以在以下位置查看类似 Type 类的代码docs http://symfony.com/doc/current/cookbook/form/data_transformers.html#using-transformers-in-a-custom-field-type.
我用 XDebug 发现buildView
for VariantType
已被叫入Purchase
's buildView
with text
表格类型。但在那之后从某个地方buildView
for VariantType
再次被调用,在这种情况下它有entity
表格类型。怎么可能?我试图在中定义空数组choices
and preferred_choices
在我的每种表单类型上,但它没有改变任何东西。我需要做什么来预防EntityChoiceList
为我的表格加载?