Injecting Dependencies into a Magento 2 CRUD/Abstract Model

First of all the constructor is private api of class. Constructor function have special meaning and do not require have to have same list/order of arguments as in parent class.

Is it possible to inject a dependency into a Magento 2 CRUD model?

Yes of course.

Is this safe?

Yes, but Magento Object Manager assume that all optional parameters is placed at end of list and required parameters after optional will not resolved.

$resource, $resourceCollection arguments is legacy but still widely used in Model classes. Most of model use code like this to initialize resource and collection class.

protected function _construct() { 
    $this->_init('Magento\AdminNotification\Model\Resource Model\Inbox'); 
}

This is why this parameters is optional. But, for example, in unit test we pass resource or collection mock in constructor to allow replace realization.


This appears to be safe. At least, magento is doing this in a number of places. See the __construct methods in the following (non exclusive) list of classes for examples

  • \Magento\Theme\Model\Theme\File
  • \Magento\Theme\Model\Design
  • \Magento\Sales\Model\Order\Creditmemo

Unfortunately, I can't answer the other part of your question.


  1. How do you use your model?
  2. In your case $mine is a required parameter, while $resource, $resourceCollection and $data are optional. Optional parameters should always go last, otherwise it's just impossible to work with them as with optional. So it looks OK to me that you should specify $mine before any optional parameters.