How-to: Optimize Symfony's forms' performance?

前端 未结 2 968
鱼传尺愫
鱼传尺愫 2021-02-18 16:14

I have a form that is the bottleneck of my ajax-request.

    $order = $this->getDoctrine()
        ->getRepository(\'AcmeMyBundle:Order\')
        ->fin         


        
相关标签:
2条回答
  • 2021-02-18 16:36

    I also had the same problem with the entity type, I needed to list cities, there were like mire then 4000, what I did basically is to inject the choices into the form. In your controller you ask the Variants from the database, in a repository call, hydrate them as array, and you select only the id and the name, or title, and then you pass into the form, as options value. With this the database part will be much quicker.

    0 讨论(0)
  • 2021-02-18 16:50

    The described behavior looks as the work of the guesser. I have the feeling that there is need to show an some additional code (listeners, VariantType, WebsiteType, PartnerType).

    Let's assume a some class has association variant to Variant and FormType for this class has code ->add('variant') without explicit specifying type (as I see there is a lot of places where the type is not specified). Then DoctrineOrmTypeGuesser comes in the game.

    https://github.com/symfony/symfony/blob/2.7/src/Symfony/Bridge/Doctrine/Form/DoctrineOrmTypeGuesser.php#L46

    This code assign the entity type (!) to this child. The EntityRepository::findAll() is called and all variants from DB are hydrated.

    As for another form optimization ways:

    • Try to specify type in all possible cases to prevent a type guessing;
    • Use SELECT with JOINs to get an order as new sub-requests to DB are sent to set an underlying data for an every form maps relation;
    • Preserve keys for collection elements on a submission as a removing of a single element without a keys preserving will trigger unnecessary updates.
    0 讨论(0)
提交回复
热议问题