9

I am trying to create a custom module in M 2 0.74.0-beta16. How can I can get collection of custom table. I tried $this->_mymodulemodelFactory->create()->getCollection();. The factory file is generated for my module but getCollection() gives noting.

  • Anna Völkl's answer working in magento 2.3.1 – Vashy Jul 25 '19 at 11:07
24

You need to inject the model collection factory into your constructor

protected $mymodulemodelFactory;
public function __construct(
    ....
    \[Namespace]\[Module]\Model\ResourceModel\[Entity]\CollectionFactory $mymodulemodelFactory, 
    ...
)
{
    ...
    $this->mymodulemodelFactory = $mymodulemodelFactory;
    ...
}

and you can use in any one of the class methods:

$collection = $this->mymodulemodelFactory->create();
|improve this answer|||||
  • i tried the same for my custom import for getting the product collection, but when i import the csv, the ajax loading symbol loads indefinitely @Marius – Sushivam Aug 8 '16 at 8:03
  • I want to get the product collection based on the entity id and save those entity ids to my custom table – Sushivam Aug 8 '16 at 8:06
18

You need to create three files for getting collection from custom module

1) Model, 2) ResourceModel, 3) Collection

Namespace: Prince

Module: Hello

Table: hello

1) Create Hello.php Model file at

app/code/Prince/Helloworld/Model/Hello.php

<?php

namespace Prince\Helloworld\Model;

use Magento\Framework\Model\AbstractModel;

class Hello extends AbstractModel
{
    /**
     * Define resource model
     */
    protected function _construct()
    {
        $this->_init('Prince\Helloworld\Model\ResourceModel\Hello');
    }
}

2) Now create Hello.php ResourceModel at

app/code/Prince/Helloworld/Model/ResourceModel/Hello.php

<?php

namespace Prince\Helloworld\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class Hello extends AbstractDb
{
    /**
     * Define main table
     */
    protected function _construct()
    {
        $this->_init('hello', 'id'); //hello is table of module
    }
}

3) Now create Collection.php Collection file at

app/code/Prince/Helloworld/Model/ResourceModel/Hello/Collection.php

<?php

namespace Prince\Helloworld\Model\ResourceModel\Hello;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
{
    /**
     * Define model & resource model
     */
    protected function _construct()
    {
        $this->_init(
            'Prince\Helloworld\Model\Hello',
            'Prince\Helloworld\Model\ResourceModel\Hello'
        );
    }
}

Now you can test your collection in Index.php controller app/code/Prince/Helloworld/Controller/Index/Index.php

<?php

namespace Prince\Helloworld\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Prince\Helloworld\Model\HelloFactory;

class Index extends Action
{
    protected $_modelHelloFactory;

    public function __construct(
        Context $context, 
        HelloFactory $modelHelloFactory
    ) {
        parent::__construct($context);
        $this->_modelHelloFactory = $modelHelloFactory;
    }

    public function execute()
    {

        $resultPage = $this->_modelHelloFactory->create();
        $collection = $resultPage->getCollection(); //Get Collection of module data
        var_dump($collection->getData());
        exit;

    }
}

If you want to get collection in template using block file

Another four files you need to add nad create

1. Block File

2. Layout File

3. Controller File

4. Templarte File

Create Block file Hello.php

namespace vendorName\ModuleName\Block\Index;

use Magento\Framework\App\RequestInterface;
use vendorName\ModuleName\Model\Hello;

class Hello extends \Magento\Framework\View\Element\Template 
{
    protected $helloModelFactory;
    protected $request;

    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        Hello $helloModelFactory,
        RequestInterface $request,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_helloModelFactory = $helloModelFactory;
        $this->_request = $request;
    }

    /**
     * Preparing global layout
     *
     * @return $this
     */
    protected function _prepareLayout() {
        parent::_prepareLayout();
        $this->pageConfig->getTitle()->set(__('Display Collection'));
        return $this;
    }

    public function getHello()
    {
        $collection = $this->_helloModelFactory->getCollection();
        return $collection;
    }
}

Or Create Layout file routName_controllername_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
             <block class="vendorName\ModuleName\Block\Event\Index" name="newevent" template="vendorName_ModuleName::Index.phtml" />
        </referenceContainer>
    </body>
</page>

Create Controller File Index.php

<?php
namespace vendorName\ModuleName\Controller\Index;

use Magento\Framework\App\Action;
use Magento\Framework\View\Result\PageFactory;

class Index extends \Magento\Framework\App\Action\Action 
{
    /**
     * @param Action\Context $context
     */

    protected $resultPageFactory;

    public function __construct(
        Action\Context $context,
        PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    public function execute() {

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

Create Template file Index.phtml

<?php

var_dump($block->getgetHello()->getData());

I hope it's work for ever

|improve this answer|||||
  • use Prince\Helloworld\Model\HelloFactory; where is this? – jibin george Nov 12 '19 at 9:40
  • got it sorry for that – jibin george Nov 12 '19 at 9:41
  • HOw do i get the full data into a array? you are var_dumping the data is there anyway i could get the full data in to array? – jibin george Nov 12 '19 at 9:42
1

try this $collection = $this->_mymodulemodelFactory->create()

|improve this answer|||||
1

I know it is a little old thread, but the best solution to get model and collection with a simple inject:

  • make sure, that your Model implements your ModelInterface, and you set the preference for your ModelInterface in di.xml

<preference for="Your\Module\Api\Data\ModelInterface" type="Your\Module\Model\Model"/>

  • Inject ModelInterfaceFactory to the constructor

  protected $_modelFactory;   public function __construct( .... \Your\Module\Api\Data\ModelInterfaceFactory $modelFactory, ... ) { ... $this->_modelFactory = $modelFactory; ... }

  • and just use anywhere in the class

    for collection:

    $collection = $this->_modelFactory->create()->getCollection();

    for model:

    $model = $this->_modelFactory->create();

|improve this answer|||||

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.