'Fatal error: Out of memory (allocated 1872756736) (tried to allocate 262144 bytes)

This is my Form

    <?php

namespace Admin\Form;
use Admin\Filter\ArticleAddInputFilter;
use Doctrine\Persistence\ObjectManager;
use Laminas\Form\Element\Checkbox;
use Laminas\Form\Element\Submit;
use Laminas\Form\Element\Text;
use Laminas\Form\Element\Textarea;
use Laminas\Form\Form;
use DoctrineModule;


class ArticleAddForm extends Form
{

    protected $objectManager;

    public function setObjectManager(ObjectManager $objectManager): void
    {
        $this->objectManager = $objectManager;
    }

    public function getObjectManager()
    {
        return $this->getObjectManager();
    }
    public function __construct(ObjectManager $objectManager)
    {
        parent::__construct('arrayAddForum');
        $this->setObjectManager($objectManager);
        $this->createElements();
    }
    public function createElements()
    {
        $this->setAttribute('method', 'POST');
        $this->setAttribute('class', 'bs-example form-horizontal');

        $this->setInputFilter(new ArticleAddInputFilter());

        $this->add([
            'type' => DoctrineModule\Form\Element\ObjectSelect::class,
            'name' => 'category',
            'options' => [
                'label' => 'Категория',
                'empty_option' => 'Выберите категорию...',
                'object_manager' => $this->getObjectManager(),
                'target_class' => 'Blog\Entity\Category',
                'property' => 'categoryName',
            ],
            'attributes' => [
             'class' => 'form-control',
             'required' => true,
            ]
        ]);
        $this->add([
            'name' => 'title',
            'type' => Text::class,
            'options' => [
                'min' => 3,
                'max' => 100,
                'label' => 'Заголовок'
            ],
            'attributes' => [
                'class' => 'form-control',
                'required' => true
            ]
        ]);
        $this->add([
            'name' => 'shortArticle',
            'type' => Textarea::class,
            'options' => [
                'label' => 'Начало статьти',
            ],
            'attributes' => [
                'class' => 'form-controller ckeditor'
            ],
        ]);
        $this->add([
            'name' => 'article',
            'type' => Textarea::class,
            'options' => [
                'label' => 'Статья',
            ],
        ]);
        $this->add([
            'name' => 'isPublic',
            'type' => Checkbox::class,
            'options' => [
                'label' => 'Опубликовать',
                'use_hidden_Element' => true,
                'checked_value' => 1,
                'unchecked_value' => 0
            ],
        ]);
        $this->add([
            'name' => 'submit',
            'type' => Submit::class,
            'attributes' => [
                'value' => 'Сохранить',
                'id' => 'btn_submit',
                'class' => 'btn btn-primary'
            ]
        ]);
    }
}

This is my controller :

    public function addAction()
{
    $em = $this->entityManager;

    $form = new ArticleAddForm($em);
    $request = $this->getRequest();
    if($request->isPost())
    {
        $article = new Article();
        $form->setHydrator(new DoctrineHydrator($em, '\Article'));
        $form->bind($article);
        $form->setData($request->getPost());
        if($form->isValid())
        {
            $em->persist($article);
            $em->flush();
        }
    }else {
        return ['form' => $form];
    }
    return $this->redirect()->toRoute('admin/article');
}

On exit i am have this error Fatal error: Allowed memory size of 524288000 bytes exhausted (tried to allocate 262144 bytes) in . If you help me, I will be very kind and grateful. I 've been working on this error for a very long time , I 've already tried everything , nothing works . Maybe I started an infinite loop somewhere , but it 's not . I 've already tried changing the configuration .htaccess and php.ini, did not work



Solution 1:[1]

There is an infinite loop in your code. I think that’s what Aleksey Blossom is trying to say.

This part

public function getObjectManager()
{
    return $this->getObjectManager();
}

This method is calling itself again, over and over.

I am not sure about the syntax but you'll need to return a value or call an other method. Any method that only calls itself will end up looping infinitely.

Hope this helps, if it did please upvote Aleksey’s answer (and this one if you think it helps) as I only clarified his answer.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Y. Gherbi