'Symfony 4 Sonata FormBuilder skip loading some relations
I've inherited an older project to provide maitanance. I don't have Symfony experience, I am coming from Laravel/Yii experience.
I've tracked down a certain ajax call that takes 30 seconds to this segment:
$formBuilder = $admin->getFormBuilder();
$form = $formBuilder->getForm();
$form->setData($subject);// <- this call takes more than 9 seconds
$form->handleRequest($admin->getRequest());
The #subject in this context is Entity\Product which has lots of relations. I debugged the code until I understood that this code block loads ALL the relations of the product
, such as articles, documents linked to the product, related products, which are a long list to load. The output is more than 24MB in size.
Now that I am newbie to Symfony I don't know what to optimize, or how to start reducing this load on this script.
But I know, that I want to skip certain relations, so they should not be loaded.
For example this is the document definition
/**
* @var ProductDocument[]|Collection
* @ORM\OneToMany(
* targetEntity="ProductDocument",
* mappedBy="product",
* cascade={"persist"}
* )
* @Sonata\ListMapper
* @Sonata\Order\ShowMapperExclude
* @Sonata\FormMapper(
* with="ProductDocument",
* type="sonata_type_collection",
* options={
* "by_reference"=false,
* "required"=false
* },
* fieldDescriptionOptions={
* "edit" = "inline",
* "inline" = "table"
* }
* )
*/
protected $documents;
How to instruct the form builder, to skip the documents, we should not load it at all? Any advice would be helpful.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|