6

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 at 11:07
18

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();
  • 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
13

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;

    }
}
1

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

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();

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.