0

I'm trying to create a custom module with the usual model, resourcemodel and collection.

My problem is that whenever I try to use the collection, magento runs out of memory. The collection only has one row and no more than 10 columns. I have increased the memory limit to 4G.

I have done this several times in earlier versions of magento (2.3.3) and never had such problems. Right now I'm on Magento 2.4.2.

Here are my files :

Collection :

<?php

namespace Vendor\Module\Model\ResourceModel\Offer;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
    protected function _construct()
    {
        $this->_init('Vendor\Module\Model\Offer', 'Vendor\Module\Model\ResourceModel\Offer');
    }
}

Model :

<?php


namespace Vendor\Module\Model;


class Offer extends \Magento\Framework\Model\AbstractModel
{

    protected function _construct()
    {
        $this->_init('Vendor\Module\Model\Offer');
    }

}

ResourceModel :

<?php
namespace Vendor\Module\Model\ResourceModel;

class Offer extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    protected function _construct()
    {
        $this->_init('vendor_module_offer', 'offer_id');
    }
}

And how I try to use it :

TestBlock.php

<?php

namespace Vendor\Module\Block\Adminhtml;

use Magento\Directory\Helper\Data as DirectoryHelper;
use Magento\Backend\Block\Template;
use Magento\Framework\Json\Helper\Data as JsonHelper;

class TestBlock extends \Magento\Backend\Block\Template
{
    private \Vendor\Module\Model\ResourceModel\Offer\CollectionFactory $_offerCollectionFactory;

    public function __construct(
        \Vendor\Module\Model\ResourceModel\Offer\CollectionFactory $offerCollectionFactory,
        Template\Context $context,
        array $data = [],
        ?JsonHelper $jsonHelper = null,
        ?DirectoryHelper $directoryHelper = null
    ) {
        $this->_offerCollectionFactory = $offerCollectionFactory;
        parent::__construct($context, $data, $jsonHelper, $directoryHelper);
    }

    public function getOffers()
    {
        return $this->_offerCollectionFactory->create()->getFirstItem()->getData();
    }
}

test.phtml

<?php
/**
 * @var $block Vendor\Module\Block\Adminhtml\TestBlock
 */

echo $block->getOffers();


And finally, the error I'm getting : enter image description here

Any help is appreciated

3
  • try var_dump(echo $block->getOffers()); or try print_r(echo $block->getOffers()); Jun 6 at 12:14
  • @GohilRajesh you can't var_dump(echo ...), and I've tried both var_dump($block->getOffers()) and print_r($block->getOffers()) and I get the same result. Jun 6 at 12:22
  • ohh.. its my mistake putted echo inside var dump Jun 6 at 12:23
0

I experienced the same issue with memory leaking. In this sitution following links help for me. I hope you also helpful following links

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 20480 bytes) in Magento customer Create

Magento 2.2.2 : Allowed memory size of 792723456 bytes exhausted (tried to allocate 184320 bytes)

2
  • Thanks, but sadly these aren't working for me. My memory limit is set correctly, I could set it to 10G and it would still crash Jun 6 at 13:17
  • Did you set memory limit in php level ? Could you check log in your project by using logging this code ini_get('memory_limit'). Sometimes it not be in 10G
    – Singhe
    Jun 6 at 14:48
0

Ok this was dumb.

The error comes from my model's $this->_init(). I actually put my model path instead of the required resourceModel path.

Here's the fixed part

Model :

protected function _construct()
    {
        $this->_init('Vendor\Module\Model\ResourceModel\Offer');
    }
1
  • happy to hear u found solution Jun 6 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.