Deprecated save and load methods in Abstract Model

40

8

I see that in the develop branch of the Magento 2 repo the methods load and save from Magento\Framework\Model\AbstractModel class are deprecated.
But there are a gazillion classes in the core that extend this class and use save and load.
When creating my own module for the CRUD part of my entities I follow the same guidelines as a core module does.
But since these methods are deprecated I would rather be prepared for the future.
What should I use instead of them? Or I should extend something else?

Marius

Posted 2016-05-11T14:01:15.013

Reputation: 150 497

Answers

21

You should use Module Service Contract.

For example for product you should use ProductRepositoryInterface

If Module Service Contract is not available you can use ResourceModel to save entities.

KAndy

Posted 2016-05-11T14:01:15.013

Reputation: 13 549

I see. This makes sense. But can you confirm that all the core CRUD modules will have service contracts at one point? – Marius – 2016-05-12T06:10:16.227

In long term perspective all modules will have Service Contract. It's our goal. (but it's my personal opinion, i do not represent officially Magento ;) – KAndy – 2016-05-12T06:53:25.240

good enough for me. :) – Marius – 2016-05-12T07:18:20.403

1I see that the implementation of ProductRepositoryInterface still uses load in the methods get and getById. Should I use the resource model for my module instead of this load method? – Marius – 2016-05-12T09:05:12.450

2yes, for your module better to use ResourceModel in your Module SL – KAndy – 2016-05-12T10:39:40.377

5can you please give some example code for how we can use ResourceModel – Yogesh Karodiya – 2016-11-08T09:45:46.747

As far as my understanding goes this is not applicable to custom tables – SwAt.Be – 2017-09-17T03:41:53.930

1Do you have any examples? I looked at the official review and newsletter modules, and they are calling "save" directly. I cannot find an example of using ResourceModel. I have it defined for my module, but how to use it? – Jānis Elmeris – 2017-11-15T15:38:14.627

Link is broken, what do? – John – 2018-02-16T15:41:21.607

19

From what I understood, what is going to happen is Magento is going to switch to hydrators with extract() and hydrate() methods.

This link used to work but it seems like Magento team rolled it back: https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/Model/Entity/EntityHydrator.php

You can find the history of the commit here though: https://github.com/magento/magento2/tree/09132da06e18dde0f90aabfc962db2bc19b64f3c/lib/internal/Magento/Framework/Model/Entity

The important files are:

  • EntityHydrator.php
  • EntityMetadata.php
  • HydratorInterface.php
  • MetadataPool.php

I also suggest you check out the files under the Action folder as well as the Sequence files.

From what I understood (I may be totally wrong here):

  • the files under the Action folder are CRUD actions
  • the Sequence files are iterators ?

That was a conversation that happened a while ago (was it Alan Storm who mentionned it ? can't remember) so I'm not sure if Magento team is still going that way.

Update

From my research, the internal Magento ticket regarding this change is MAGETWO-50676, here are the related commits I managed to find:

There's probably more TBH but I don't feel like browsing the entire repo for commit messages ^^

If you're not familiar with hydrators, I suggest you check that link out: http://www.webconsults.eu/blog/entry/108-What_is_a_Hydrator_in_Zend_Framework_2

Update from 2.1

Magento is now using the EntityManager class to replace the inheritance you can find more information here: Magento 2.1: using the entity manager

Raphael at Digital Pianism

Posted 2016-05-11T14:01:15.013

Reputation: 47 283

1Ok. Nice theory. But I could use an example from the core. I'm sorry, but my magento skills resume to copy/paste/replace :). You mentioned Action and Sequence files. Can you be more specific? – Marius – 2016-05-11T14:14:08.993

@Marius unfortunately that's all I know. I can't remember where I got that info from but the plan at that time was to use this particular commit: https://github.com/magento/magento2/tree/09132da06e18dde0f90aabfc962db2bc19b64f3c/lib/internal/Magento/Framework/Model/Entity to implement the switch from load()/save() to hydrators. I assume Sequences work like iterators and Actions are CRUD actions

– Raphael at Digital Pianism – 2016-05-11T14:16:34.217

3

you find an example in the current cms block resource model load method: https://github.com/magento/magento2/blob/develop/app/code/Magento/Cms/Model/ResourceModel/Block.php#L137 It uses the entityManager->load https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/EntityManager/EntityManager.php#L61 which executes a ReadMain operation (I think) https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/EntityManager/Operation/Read/ReadMain.php#L64 which hydrates the skeleton entity with the loaded entity data (nice move from Magento ;))

– David Verholen – 2016-05-17T19:23:50.257

2

Anton Kril

Posted 2016-05-11T14:01:15.013

Reputation: 3 626

1Any idea when load save and delete are going to go away? – Marius – 2017-06-22T06:02:12.160

1They will stay for 2-3 minor releases – Anton Kril – 2017-06-24T19:53:38.127