Memory usage exceeds limit - with nested fieldsets & doctrine

Hi,

I get the error “Memory usage exeeds limit” on validating the form containing nested fieldsets:

#0  ...vendor\laminas\laminas-cache\src\Storage\Adapter\AbstractAdapter.php(664): Laminas\Cache\Storage\Adapter\Memory->internalSetItem('[News_...', Object(Doctrine\ORM\Mapping\ClassMetadata))
#1  ...vendor\doctrine\doctrine-module\src\Cache\LaminasStorageCache.php(49): Laminas\Cache\Storage\Adapter\AbstractAdapter->setItem('[News_...', Object(Doctrine\ORM\Mapping\ClassMetadata))
#2  ...vendor\doctrine\cache\lib\Doctrine\Common\Cache\CacheProvider.php(259): DoctrineModule\Cache\LaminasStorageCache->doSave('[News_...', Object(Doctrine\ORM\Mapping\ClassMetadata), 0)
#3  ...vendor\doctrine\cache\lib\Doctrine\Common\Cache\CacheProvider.php(99): Doctrine\Common\Cache\CacheProvider->doSaveMultiple(Array, 0)
#4  ...vendor\doctrine\cache\lib\Doctrine\Common\Cache\Psr6\CacheAdapter.php(330): Doctrine\Common\Cache\CacheProvider->saveMultiple(Array, 0)
#5  ...vendor\doctrine\cache\lib\Doctrine\Common\Cache\Psr6\CacheAdapter.php(240): Doctrine\Common\Cache\Psr6\CacheAdapter->doSaveMultiple(Array, 0)
#6  ...vendor\doctrine\persistence\src\Persistence\Mapping\AbstractClassMetadataFactory.php(222): Doctrine\Common\Cache\Psr6\CacheAdapter->commit()
#7  ...vendor\doctrine\orm\src\EntityManager.php(329): Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor('News\\E...')
#8  ...vendor\doctrine\doctrine-laminas-hydrator\src\DoctrineObject.php(161): Doctrine\ORM\EntityManager->getClassMetadata('News\\E...')
#9  ...vendor\doctrine\doctrine-laminas-hydrator\src\DoctrineObject.php(131): Doctrine\Laminas\Hydrator\DoctrineObject->prepare(Object(News\Entity\StandardcontentEntity))
#10 ...vendor\laminas\laminas-form\src\Fieldset.php(615): Doctrine\Laminas\Hydrator\DoctrineObject->extract(Object(News\Entity\StandardcontentEntity))
#11 ...vendor\laminas\laminas-form\src\Fieldset.php(544): Laminas\Form\Fieldset->extract()
#12 ...vendor\laminas\laminas-form\src\Element\Collection.php(281): Laminas\Form\Fieldset->bindValues(Array, NULL)
#13 ...vendor\laminas\laminas-form\src\Fieldset.php(567): Laminas\Form\Element\Collection->bindValues(Array, NULL)
#14 ...vendor\laminas\laminas-form\src\Form.php(352): Laminas\Form\Fieldset->bindValues(Array, Array)
#15 ...vendor\laminas\laminas-form\src\Form.php(478): Laminas\Form\Form->bindValues()
#16 ...module\...: Laminas\Form\Form->isValid()

The entity to be saved is a NewsEntity with nested ContentEntitis (4 Entities in total):

  • NewsEntity
    • ContentEntity
      • ContentEntity
      • ContentEntity

That should be manageable, right? My assosiations all have the property “fetch: ‘EXTRA_LAZY’”.

PHP 8.2, doctrine-orm-module: 6.2.0, laminas-mvc 3.8.0

Hi MaSa759,
when I deal with doctrine memory usage, I usually call the clear method over the entity involved to save some memory:

$model->getEntityManager()->clear(MyEntity::class);

a little bit more detail: The ContentEntity can have up to 4 children (also ContentEntities)

    #[ORM\HasLifecycleCallbacks]
    class ContentEntity extends AbstractContentElement
    {
        // ...
    
        #[ORM\JoinColumn(name: 'parent1_id', referencedColumnName: 'id')]
        #[ORM\ManyToOne(targetEntity: ContentEntity::class, inversedBy: 'col1', fetch: 'EXTRA_LAZY')]
        protected ?AbstractContentElement $parent1 = null;    	
        ...
        protected ?AbstractContentElement $parent2 = null;        
    	...
        protected ?AbstractContentElement $parent3 = null;        
    	...
        protected ?AbstractContentElement $parent4 = null;


        #[ORM\OneToMany(targetEntity: ContentEntity::class, mappedBy: 'parent1', fetch: 'EXTRA_LAZY', cascade: ['ALL'])]
        protected Collection $col1;        
    	...
        protected Collection $col2;        
    	...
        protected Collection $col3;        
    	...
        protected Collection $col4;

This relates to an ContentFieldset with 4 collections like this

    $this->add(
        [
            'type'    => Collection::class,
            'name'    => 'col1', // ..2,3,4
            'options' => [
                'label'                  => _('label_fieldset_standardcontent'),
                'count'                  => 0,
                'should_create_template' => false,
                'allow_add'              => true,
                'min'                    => 0,
                'target_element'         => [
                    'type' => ContentFieldset::class,
                ],
            ],
        ]
    );

Removing the fieldsets for col3 and col4 solves the issue. It seamingly does not matter, how many entities are to be persisted (col3 and col4 had no entity-relations in my case). So doctrine (?) seems to struggle with the complexity of the fieldset during the hydration process?