3

Looking at some of the Resource Models in Magento 2, you will find the following class definitions

namespace Magento\Catalog\Model\ResourceModel;

abstract class AbstractResource extends \Magento\Eav\Model\Entity\AbstractEntity
{
   ...
}

namespace Magento\Customer\Model\ResourceModel;

class Customer extends \Magento\Eav\Model\Entity\VersionControl\AbstractEntity
{
    ...
}

class Group extends \Magento\Framework\Model\ResourceModel\Db\VersionControl\AbstractDb
{
    ...
}

class Visitor extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    ...
}

As you can see, there are VersionControl abstract classes and regular ones.

\Magento\Eav\Model\Entity\AbstractEntity
\Magento\Eav\Model\Entity\VersionControl\AbstractEntity
\Magento\Framework\Model\ResourceModel\Db\AbstractDb
\Magento\Framework\Model\ResourceModel\Db\VersionControl\AbstractDb

What is the difference between the two? When you create your own entities, should your Resource Models extend from the VersionControl class or the regular one?

1 Answer 1

4

So just to clarify, in Magento 2:

  • \Magento\Eav\Model\Entity\VersionControl\AbstractEntity extends \Magento\Eav\Model\Entity\AbstractEntity
  • \Magento\Framework\Model\ResourceModel\Db\VersionControl\AbstractDb extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb

The VersionControl classes have the following extra functionalities:

  • After loading and after saving an entity, they register a snapshot of entity data \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot, for tracking changes. That snapshot is used with the extra isModified function to check if entity was modified in the save method.
  • When saving an entity, a \Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite is used to process model's relations saves before and after saving the object.

According to the Magento U fundamental course, when creating your own entities, you should use the \Magento\Eav\Model\Entity\AbstractEntity and the \Magento\Framework\Model\ResourceModel\Db\AbstractDb. The VersionControl classes are never mentionned over the course.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.