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;>getCollection();
for model:
$model = $this->_modelFactory->create();