Dive in thousand classes , interfaces maybe more.
How to decide which one to extend while create model or constructors.. to build module from scratch
Sometimes it makes me so confuse. It too many
Anyone can give me advice
-
The question is too broad, the answer depends on what exactly you are trying to achieve.– Alex PaliarushDec 13, 2015 at 19:09
-
Not affiliated in any way, but Alan Storm's commerce bug can be a lifesaver in terms of navigating which blocks, models, observers, etc.. store.pulsestorm.net/products/commerce-bug-2 Agree with @AlexPaliarush, if there's something particular that you need, reframe the question.– Phil BirnieDec 14, 2015 at 2:12
1 Answer
Here are the classes you need to extend:
Magento\Framework\Model\AbstractModel
for flat table modelMagento\Framework\Model\AbstractExtensibleModel
for eav modelsMagento\Framework\Model\ResourceModel\Db\AbstractDb
for flat table resource modelsMagento\Eav\Model\Entity\AbstractEntity
for eav resource modelsMagento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
for flat entity collection resource modelMagento\Eav\Model\Entity\Collection\AbstractCollection
for eav entities collection resource modelMagento\Backend\App\Action
for the admin actions.
As for the constructor, if your class does not have any other dependencies that the parent class, don't bother writing it.
If you have other dependencies, then just copy the original class constructor and inside it add just parent::__construct(dependencies here)
then start adding your additional dependencies.
Let's take an example: You are extending Magento\Framework\Model\AbstractModel
and you want to add your dependencies.
The constructor for Magento\Framework\Model\AbstractModel
looks like this:
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
$this->_registry = $registry;
$this->_appState = $context->getAppState();
$this->_eventManager = $context->getEventDispatcher();
$this->_cacheManager = $context->getCacheManager();
$this->_resource = $resource;
$this->_resourceCollection = $resourceCollection;
$this->_logger = $context->getLogger();
$this->_actionValidator = $context->getActionValidator();
if (method_exists($this->_resource, 'getIdFieldName')
|| $this->_resource instanceof \Magento\Framework\DataObject
) {
$this->_idFieldName = $this->_getResource()->getIdFieldName();
}
parent::__construct($data);
$this->_construct();
}
you copy it in your class and transform it to this:
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
parent::__construct($context, $registry, $resource, $resouceCollection, $data)
}
Now, let's say your class requires a new dependency. SomeClass $someClass
You can add it anywhere in the constructor parameters, but make sure you add it above the parameters with default values.
I recommend adding it on top of all the parameters, to make it clearer.
So your class constructor becomes:
public function __construct(
SomeClass $someClass, //added this dependency
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
$this->someClass = $someClass;//added this dependency
parent::__construct($context, $registry, $resource, $resouceCollection, $data)
}
Now make sure you create a protected class member called someClass
.