Skip to content
Advertisement

Adding collections/entities makes Form Rendering terrible slow

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.

Profiler 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?

Advertisement

Answer

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' => 'MyCompanyAppBundleEntityLessor',
            'property' => 'title')
        )
        ->add('type', 'entity', array(
            'class' => 'MyCompanyAppBundleEntityObjectType',
            '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 Example of Xhprof

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement