I'm using the Symfony2 Form Builder to make a form. All is working well, but the performance is terrible. A simple form (just a couple of text fields) is rendering in about 1000 ms, but with a entity or collection field it's slowing down to about 7500-10000 ms.
I'm using the query_builder
(with createQueryBuilder()
) option, like this example from the documentation. Adding a collection or entity field makes the application terrible slow, but I don't know why.
The Controller takes most of the time, so the Doctrine part (192 queries) or the Twig part doesn't seem to be the problem. Stripping the twig template doesn't help. I tried some improvements (caching) already, but that didn't help either.
How can I improve (or: see where's the problem exactly) the performance of this piece of code?
The way your doing your form entity field and collection is bad and your performance issue may come from this.
First try to remove the queries you write in your forms, this is not the proper place especially with your queries which are the default one so no need to override them.
So you can do very simply like this :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('ref', 'text')
->add('title', 'text')
->add('lessor', 'entity', array(
'class' => 'MyCompany\AppBundle\Entity\Lessor',
'property' => 'title')
)
->add('type', 'entity', array(
'class' => 'MyCompany\AppBundle\Entity\ObjectType',
'property' => 'title')
)
->add('prices', 'collection', array(
'type' => new ObjectItemPriceType()
))
->add('values', 'collection', array(
'type' => new ObjectValueTextType()
'allow_add' => true,
'prototype' => true
))
->add('save', 'submit');
}
Learn more about entity form type here
If you need to pass some choices like it seems you want to do, you will have to set the query_builder option and give it a repository method (Example here), this is the correct way to do.
For the collection, you have a full example in the cookbook, I hope it will help you.
If it does not solve you problem, try to setup xhprof, it will give you a more detailed graph of your request and why it takes so much time : Xhprof bundle
You should use data transformers http://symfony.com/doc/current/cookbook/form/data_transformers.html
Could you provide more info about your template (twig files). 2 seconds on rendering looks suspicious. And also check it again with disabled ANY profilers (e.g. xdebug).
If bad perfomance still exist (with disabled profiler) enable it and try to find slowest function and improve it, Xdebug could help you with it. I know it is huge, slow and ugly but it is the simplest way to do some measurements if you didn't want to install any other
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With