php - Memory management Symfony forms and Doctrine -
php - Memory management Symfony forms and Doctrine -
when building form using symfony build of form terribly slow , memory spikes.
the form build using subforms , uses one-to-many
relations. when info of form becomes larger (more entities in many side) form slower , memory usage getting larger seem okey though amount of time , memory usage don't seem to.
example when having 71 enities in many side memory usage 116 mb , takes 14 seconds load.
i deduced number of queries done (from 75 4) though memory spike still happens moment form created
$form = $this->createform(new tapsandappliancestype(), $taps);
any tips , tricks speed up?
i assume utilize type entity
in form. quite heavy, since first entities fetched objects , reduced id => label
style.
so write own entitychoice
type, works id => label
-array (so nil fetched object in frist place) , add together datatransformer type:
use symfony\component\form\abstracttype; utilize symfony\component\form\formbuilderinterface; utilize symfony\component\optionsresolver\optionsresolverinterface; utilize mynamespace\entitytoidtransformer; class entitychoicetype extends abstracttype { public function buildform(formbuilderinterface $builder, array $options) { $builder->addmodeltransformer(new entitytoidtransformer($options['repository'])); } public function setdefaultoptions(optionsresolverinterface $resolver) { $resolver->setdefaults(array( 'empty_value' => false, 'empty_data' => null, )); $resolver->setrequired(array( 'repository' )); } public function getparent() { homecoming 'choice'; } public function getname() { homecoming 'entitychoice'; } }
and datatransformer:
use doctrine\orm\entityrepository; utilize symfony\component\form\datatransformerinterface; utilize symfony\component\form\exception\transformationfailedexception; class entitytoidtransformer implements datatransformerinterface { private $entityrepository; public function __construct(entityrepository $entityrepository) { $this->entityrepository = $entityrepository; } /** * @param object|array $entity * @return int|int[] * * @throws transformationfailedexception */ public function transform($entity) { if ($entity === null) { homecoming null; } elseif (is_array($entity) || $entity instanceof \doctrine\orm\persistentcollection) { $ids = array(); foreach ($entity $subentity) { $ids[] = $subentity->getid(); } homecoming $ids; } elseif (is_object($entity)) { homecoming $entity->getid(); } throw new transformationfailedexception((is_object($entity)? get_class($entity) : '').'('.gettype($entity).') not valid class entitytoidtransformer'); } /** * @param int|array $id * @return object|object[] * * @throws transformationfailedexception */ public function reversetransform($id) { if ($id === null) { homecoming null; } elseif (is_numeric($id)) { $entity = $this->entityrepository->findoneby(array('id' => $id)); if ($entity === null) { throw new transformationfailedexception('a '.$this->entityrepository->getclassname().' id #'.$id.' not exist!'); } homecoming $entity; } elseif (is_array($id)) { if (empty($id)) { homecoming array(); } $entities = $this->entityrepository->findby(array('id' => $id)); // array('id' => array(...)), resulting in many entities!! if (count($id) != count($entities)) { throw new transformationfailedexception('some '.$this->entityrepository->getclassname().' ids #'.implode(', ', $id).' not exist!'); } homecoming $entities; } throw new transformationfailedexception(gettype($id).' not valid type entitytoidtransformer'); } }
and register formtype new type in service.yml
services: mynamespace.form.type.entitychoice: class: mynamespace\entitychoicetype tags: - { name: form.type, alias: entitychoice }
you can utilize in form as
$formbuilder->add('appliance', 'entitychoice', array( 'label' => 'my label', 'repository' => $repository, 'choices' => $repository->getlabelsbyid(), 'multiple' => false, 'required' => false, 'empty_value' => '(none)', ))
with $repository
instance of desired repository , 'choices'
array id => label
php forms symfony2 memory doctrine2
Comments
Post a Comment