Skip to main content
removed superfluous code
Source Link
<?php
namespace Customer\Customerimportexport\Controller\Adminhtml\Import;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Customer\Customerimportexport\Model\WebcustomersFactory;

class Index extends \Magento\Backend\App\Action
{
    
    protected $_modelWebcustomersFactory;

    public function __construct(
        Context $context,
        WebcustomersFactory $modelWebcustomersFactory
    ) {
        parent::__construct($context);
        $this->_modelWebcustomersFactory = $modelWebcustomersFactory;
    }

    public function execute()
    {

        $webcustomersModel = $this->_modelWebcustomersFactory->create();
 
        // Load the item with ID is 1
        /*$item = $webcustomersModel->load(976);
        var_dump($item->getData());*/
 
        // Get webcustomers collection
        $webcustomersCollection = $webcustomersModel->getCollection();

        // Load all data of collection
        foreach ($webcustomersModel->getCollection() as $key => $webcustomer) {
            var_dump($webcustomer->getData());
            die();
        }

        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }

}
<?php
namespace Customer\Customerimportexport\Controller\Adminhtml\Import;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Customer\Customerimportexport\Model\WebcustomersFactory;

class Index extends \Magento\Backend\App\Action
{
    
    protected $_modelWebcustomersFactory;

    public function __construct(
        Context $context,
        WebcustomersFactory $modelWebcustomersFactory
    ) {
        parent::__construct($context);
        $this->_modelWebcustomersFactory = $modelWebcustomersFactory;
    }

    public function execute()
    {

        $webcustomersModel = $this->_modelWebcustomersFactory->create();
 
        // Load the item with ID is 1
        /*$item = $webcustomersModel->load(976);
        var_dump($item->getData());*/
 
        // Get webcustomers collection
        $webcustomersCollection = $webcustomersModel->getCollection();

        // Load all data of collection
        foreach ($webcustomersModel->getCollection() as $key => $webcustomer) {
            var_dump($webcustomer->getData());
            die();
        }

        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }

}
<?php
namespace Customer\Customerimportexport\Controller\Adminhtml\Import;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Customer\Customerimportexport\Model\WebcustomersFactory;

class Index extends \Magento\Backend\App\Action
{
    
    protected $_modelWebcustomersFactory;

    public function __construct(
        Context $context,
        WebcustomersFactory $modelWebcustomersFactory
    ) {
        parent::__construct($context);
        $this->_modelWebcustomersFactory = $modelWebcustomersFactory;
    }

    public function execute()
    {

        $webcustomersModel = $this->_modelWebcustomersFactory->create();
 
        // Get webcustomers collection
        $webcustomersCollection = $webcustomersModel->getCollection();

        // Load all data of collection
        foreach ($webcustomersModel->getCollection() as $key => $webcustomer) {
            var_dump($webcustomer->getData());
            die();
        }

        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }

}
formatting
Source Link
Fabian Schmengler
  • 66.1k
  • 25
  • 188
  • 422

Controller###Controller:`<?php namespace Customer\Customerimportexport\Controller\Adminhtml\Import;

use Magento\Backend\App\Action; use Magento\Backend\App\Action\Context; use Customer\Customerimportexport\Model\WebcustomersFactory;

class Index extends \Magento\Backend\App\Action {

<?php
namespace Customer\Customerimportexport\Controller\Adminhtml\Import;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Customer\Customerimportexport\Model\WebcustomersFactory;

class Index extends \Magento\Backend\App\Action
{
    
    protected $_modelWebcustomersFactory;

    public function __construct(
        Context $context,
        WebcustomersFactory $modelWebcustomersFactory
    ) {
        parent::__construct($context);
        $this->_modelWebcustomersFactory = $modelWebcustomersFactory;
    }

    public function execute()
    {

        $webcustomersModel = $this->_modelWebcustomersFactory->create();
 
        // Load the item with ID is 1
        /*$item = $webcustomersModel->load(976);
        var_dump($item->getData());*/
 
        // Get webcustomers collection
        $webcustomersCollection = $webcustomersModel->getCollection();

        // Load all data of collection
        foreach ($webcustomersModel->getCollection() as $key => $webcustomer) {
            var_dump($webcustomer->getData());
            die();
        }

        $this->_view->loadLayout();
        $this->_view->renderLayout();
    } 

}

} ###Model

_init('Customer\Customerimportexport\Model\ResourceModel\Webcustomers');}
<?php

namespace Customer\Customerimportexport\Model;

use \Magento\Framework\Model\AbstractModel;
 
class Webcustomers extends AbstractModel
{
    /**
     * Define resource model
     */
    protected function _construct()
    {
        $this->_init('Customer\Customerimportexport\Model\ResourceModel\Webcustomers');
    }
}
}_init('web_customers', 'entity_id');}

###Resource Model

}_init('Customer\Customerimportexport\Model\Webcustomers','Customer\Customerimportexport\Model\ResourceModel\Webcustomers');}
<?php
 
namespace Customer\Customerimportexport\Model\ResourceModel;
 
class Webcustomers extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    /**
     * Define main table
     */
    protected function _construct()
    {
        $this->_init('web_customers', 'entity_id');
    }
}
}`

###Collection

<?php 

namespace Customer\Customerimportexport\Model\ResourceModel\Webcustomers;

use \Magento\Framework\Model\Resource\Db\Collection\AbstractCollection;
 
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
    /**
     * Define model & resource model
     */
    protected function _construct()
    {
        $this->_init(
            'Customer\Customerimportexport\Model\Webcustomers',
            'Customer\Customerimportexport\Model\ResourceModel\Webcustomers'
        );
    }
}

Controller:`<?php namespace Customer\Customerimportexport\Controller\Adminhtml\Import;

use Magento\Backend\App\Action; use Magento\Backend\App\Action\Context; use Customer\Customerimportexport\Model\WebcustomersFactory;

class Index extends \Magento\Backend\App\Action {

protected $_modelWebcustomersFactory;

public function __construct(
    Context $context,
    WebcustomersFactory $modelWebcustomersFactory
) {
    parent::__construct($context);
    $this->_modelWebcustomersFactory = $modelWebcustomersFactory;
}

public function execute()
{

    $webcustomersModel = $this->_modelWebcustomersFactory->create();

    // Load the item with ID is 1
    /*$item = $webcustomersModel->load(976);
    var_dump($item->getData());*/

    // Get webcustomers collection
    $webcustomersCollection = $webcustomersModel->getCollection();

    // Load all data of collection
    foreach ($webcustomersModel->getCollection() as $key => $webcustomer) {
        var_dump($webcustomer->getData());
        die();
    }

    $this->_view->loadLayout();
    $this->_view->renderLayout();
}

}

_init('Customer\Customerimportexport\Model\ResourceModel\Webcustomers');}}_init('web_customers', 'entity_id');}}_init('Customer\Customerimportexport\Model\Webcustomers','Customer\Customerimportexport\Model\ResourceModel\Webcustomers');}}`

###Controller:

<?php
namespace Customer\Customerimportexport\Controller\Adminhtml\Import;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Customer\Customerimportexport\Model\WebcustomersFactory;

class Index extends \Magento\Backend\App\Action
{
    
    protected $_modelWebcustomersFactory;

    public function __construct(
        Context $context,
        WebcustomersFactory $modelWebcustomersFactory
    ) {
        parent::__construct($context);
        $this->_modelWebcustomersFactory = $modelWebcustomersFactory;
    }

    public function execute()
    {

        $webcustomersModel = $this->_modelWebcustomersFactory->create();
 
        // Load the item with ID is 1
        /*$item = $webcustomersModel->load(976);
        var_dump($item->getData());*/
 
        // Get webcustomers collection
        $webcustomersCollection = $webcustomersModel->getCollection();

        // Load all data of collection
        foreach ($webcustomersModel->getCollection() as $key => $webcustomer) {
            var_dump($webcustomer->getData());
            die();
        }

        $this->_view->loadLayout();
        $this->_view->renderLayout();
    } 

}

###Model

<?php

namespace Customer\Customerimportexport\Model;

use \Magento\Framework\Model\AbstractModel;
 
class Webcustomers extends AbstractModel
{
    /**
     * Define resource model
     */
    protected function _construct()
    {
        $this->_init('Customer\Customerimportexport\Model\ResourceModel\Webcustomers');
    }
}

###Resource Model

<?php
 
namespace Customer\Customerimportexport\Model\ResourceModel;
 
class Webcustomers extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    /**
     * Define main table
     */
    protected function _construct()
    {
        $this->_init('web_customers', 'entity_id');
    }
}

###Collection

<?php 

namespace Customer\Customerimportexport\Model\ResourceModel\Webcustomers;

use \Magento\Framework\Model\Resource\Db\Collection\AbstractCollection;
 
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
    /**
     * Define model & resource model
     */
    protected function _construct()
    {
        $this->_init(
            'Customer\Customerimportexport\Model\Webcustomers',
            'Customer\Customerimportexport\Model\ResourceModel\Webcustomers'
        );
    }
}
added 48 characters in body
Source Link

I am writing an admin module. I created a collection and if the number of rows in the table are small it loads fine. Ultimately the table will have 250k rows in it and when I attempt to load the collection it exhausts memory. I was under the impression that Magento would lazy load collections. I also will probably need to know how to use the resource iterator in mage 2 as well. I can provide code samples but there is little more than a basic module skeleton at this point. I would greatly appreciate a code example of how to handle and iterate through a large collection.

Note: the collection is pulling from a custom table

Controller:`<?php namespace Corra\Corraimportexport\Controller\Adminhtml\Import;Customer\Customerimportexport\Controller\Adminhtml\Import;

use Magento\Backend\App\Action; use Magento\Backend\App\Action\Context; use Corra\Corraimportexport\Model\WebcustomersFactory;Customer\Customerimportexport\Model\WebcustomersFactory;

class Index extends \Magento\Backend\App\Action {

protected $_modelWebcustomersFactory;

public function __construct(
    Context $context,
    WebcustomersFactory $modelWebcustomersFactory
) {
    parent::__construct($context);
    $this->_modelWebcustomersFactory = $modelWebcustomersFactory;
}

public function execute()
{

    $webcustomersModel = $this->_modelWebcustomersFactory->create();

    // Load the item with ID is 1
    /*$item = $webcustomersModel->load(976);
    var_dump($item->getData());*/

    // Get webcustomers collection
    $webcustomersCollection = $webcustomersModel->getCollection();

    // Load all data of collection
    foreach ($webcustomersModel->getCollection() as $key => $webcustomer) {
        var_dump($webcustomer->getData());
        die();
    }

    $this->_view->loadLayout();
    $this->_view->renderLayout();
}

}

_init('Corra\Corraimportexport\Model\ResourceModel\Webcustomers''Customer\Customerimportexport\Model\ResourceModel\Webcustomers'); } } _init('web_customers', 'entity_id'); } } _init( 'Corra\Corraimportexport\Model\Webcustomers''Customer\Customerimportexport\Model\Webcustomers', 'Corra\Corraimportexport\Model\ResourceModel\Webcustomers''Customer\Customerimportexport\Model\ResourceModel\Webcustomers' ); } }`

I am writing an admin module. I created a collection and if the number of rows in the table are small it loads fine. Ultimately the table will have 250k rows in it and when I attempt to load the collection it exhausts memory. I was under the impression that Magento would lazy load collections. I also will probably need to know how to use the resource iterator in mage 2 as well. I can provide code samples but there is little more than a basic module skeleton at this point. I would greatly appreciate a code example of how to handle and iterate through a large collection.

Note: the collection is pulling from a custom table

Controller:`<?php namespace Corra\Corraimportexport\Controller\Adminhtml\Import;

use Magento\Backend\App\Action; use Magento\Backend\App\Action\Context; use Corra\Corraimportexport\Model\WebcustomersFactory;

class Index extends \Magento\Backend\App\Action {

protected $_modelWebcustomersFactory;

public function __construct(
    Context $context,
    WebcustomersFactory $modelWebcustomersFactory
) {
    parent::__construct($context);
    $this->_modelWebcustomersFactory = $modelWebcustomersFactory;
}

public function execute()
{

    $webcustomersModel = $this->_modelWebcustomersFactory->create();

    // Load the item with ID is 1
    /*$item = $webcustomersModel->load(976);
    var_dump($item->getData());*/

    // Get webcustomers collection
    $webcustomersCollection = $webcustomersModel->getCollection();

    // Load all data of collection
    foreach ($webcustomersModel->getCollection() as $key => $webcustomer) {
        var_dump($webcustomer->getData());
        die();
    }

    $this->_view->loadLayout();
    $this->_view->renderLayout();
}

}

_init('Corra\Corraimportexport\Model\ResourceModel\Webcustomers'); } } _init('web_customers', 'entity_id'); } } _init( 'Corra\Corraimportexport\Model\Webcustomers', 'Corra\Corraimportexport\Model\ResourceModel\Webcustomers' ); } }`

I am writing an admin module. I created a collection and if the number of rows in the table are small it loads fine. Ultimately the table will have 250k rows in it and when I attempt to load the collection it exhausts memory. I was under the impression that Magento would lazy load collections. I also will probably need to know how to use the resource iterator in mage 2 as well. I can provide code samples but there is little more than a basic module skeleton at this point. I would greatly appreciate a code example of how to handle and iterate through a large collection.

Note: the collection is pulling from a custom table

Controller:`<?php namespace Customer\Customerimportexport\Controller\Adminhtml\Import;

use Magento\Backend\App\Action; use Magento\Backend\App\Action\Context; use Customer\Customerimportexport\Model\WebcustomersFactory;

class Index extends \Magento\Backend\App\Action {

protected $_modelWebcustomersFactory;

public function __construct(
    Context $context,
    WebcustomersFactory $modelWebcustomersFactory
) {
    parent::__construct($context);
    $this->_modelWebcustomersFactory = $modelWebcustomersFactory;
}

public function execute()
{

    $webcustomersModel = $this->_modelWebcustomersFactory->create();

    // Load the item with ID is 1
    /*$item = $webcustomersModel->load(976);
    var_dump($item->getData());*/

    // Get webcustomers collection
    $webcustomersCollection = $webcustomersModel->getCollection();

    // Load all data of collection
    foreach ($webcustomersModel->getCollection() as $key => $webcustomer) {
        var_dump($webcustomer->getData());
        die();
    }

    $this->_view->loadLayout();
    $this->_view->renderLayout();
}

}

_init('Customer\Customerimportexport\Model\ResourceModel\Webcustomers'); } } _init('web_customers', 'entity_id'); } } _init( 'Customer\Customerimportexport\Model\Webcustomers', 'Customer\Customerimportexport\Model\ResourceModel\Webcustomers' ); } }`
remove line that was not in the original code
Source Link
Loading
added 2445 characters in body
Source Link
Loading
added 55 characters in body
Source Link
Loading
Source Link
Loading