Skip to main content
added implements
Source Link
Mammouth
  • 796
  • 10
  • 10

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

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

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

added implements
Source Link
Mammouth
  • 796
  • 10
  • 10

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 a

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

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

  • make sure, that you set preference for your ModelInterface in di.xml a

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

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

Source Link
Mammouth
  • 796
  • 10
  • 10

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

  • make sure, that you set preference for your ModelInterface in di.xml a

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