Magento 2 – How to Get Collection of a Custom Module

magento2magento2-dev-beta

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.

Best Answer

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