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?
- 197,939
- 53
- 422
- 830
-
Are these methods are deprecated now? – Knight017 Jan 18 '19 at 09:43
-
1If, by now you mean 2.3, yes they are: https://github.com/magento/magento2/blob/2.3/lib/internal/Magento/Framework/Model/AbstractModel.php#L531 – Marius Jan 18 '19 at 10:00
4 Answers
You should use Module Service Contract.
For example for product you should use ProductRepositoryInterface
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Api;
/**
* @api
* @since 100.0.2
*/
interface ProductRepositoryInterface
{
/**
* Create product
*
* @param \Magento\Catalog\Api\Data\ProductInterface $product
* @param bool $saveOptions
* @return \Magento\Catalog\Api\Data\ProductInterface
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\StateException
* @throws \Magento\Framework\Exception\CouldNotSaveException
*/
public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = false);
/**
* Get info about product by product SKU
*
* @param string $sku
* @param bool $editMode
* @param int|null $storeId
* @param bool $forceReload
* @return \Magento\Catalog\Api\Data\ProductInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function get($sku, $editMode = false, $storeId = null, $forceReload = false);
/**
* Get info about product by product id
*
* @param int $productId
* @param bool $editMode
* @param int|null $storeId
* @param bool $forceReload
* @return \Magento\Catalog\Api\Data\ProductInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getById($productId, $editMode = false, $storeId = null, $forceReload = false);
/**
* Delete product
*
* @param \Magento\Catalog\Api\Data\ProductInterface $product
* @return bool Will returned True if deleted
* @throws \Magento\Framework\Exception\StateException
*/
public function delete(\Magento\Catalog\Api\Data\ProductInterface $product);
/**
* @param string $sku
* @return bool Will returned True if deleted
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\StateException
*/
public function deleteById($sku);
/**
* Get product list
*
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
* @return \Magento\Catalog\Api\Data\ProductSearchResultsInterface
*/
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria);
}
If Module Service Contract is not available you can use ResourceModel to save entities.
-
I see. This makes sense. But can you confirm that all the core CRUD modules will have service contracts at one point? – Marius May 12 '16 at 06:10
-
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 May 12 '16 at 06:53
-
-
1I see that the implementation of
ProductRepositoryInterfacestill usesloadin the methodsgetandgetById. Should I use the resource model for my module instead of thisloadmethod? – Marius May 12 '16 at 09:05 -
2
-
6can you please give some example code for how we can use ResourceModel – Yogesh Karodiya Nov 08 '16 at 09:45
-
As far as my understanding goes this is not applicable to custom tables – CompactCode Sep 17 '17 at 03:41
-
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 Nov 15 '17 at 15:38
-
Do you have any examples of how specifically to use Resource Model i feel it would really improve your answer – John Mar 19 '18 at 16:23
-
As for question about usage of resource model, which also should work for any models, also custom ones:
$modelObject->setData($data); $resourceModelObject->save($modelObject);So, it works exactly the same like using resource model in M1. For delete:$resourceModelObject->delete($modelObject);and for loading$resourceModelObject->load($modelObject, $value, $idFieldName);where id field is optional unless you want to load the model with field other than it's ID (entity_id usually); – Lanius Mar 07 '19 at 06:50
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.phpEntityMetadata.phpHydratorInterface.phpMetadataPool.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
Actionfolder are CRUD actions - the
Sequencefiles 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:
- https://github.com/magento/magento2/commit/d57c81ced2419cde9d8af2f55062a783ec6a7789
- https://github.com/magento/magento2/commit/35d2da47a20e978c1cb970db79ee4ea60de56353
- https://github.com/magento/magento2/commit/074b3abc6803454542ff0527110e575309c42466
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
- 70,385
- 34
- 188
- 352
-
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 May 11 '16 at 14:14
-
@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 assumeSequenceswork like iterators andActionsare CRUD actions – Raphael at Digital Pianism May 11 '16 at 14:16 -
4you 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 May 17 '16 at 19:23
An alternative to Magento 2 Deprecated Load Method is the resource model load method.
public funtion getCustomer($id)
{
$customerModel = $this->customerFactory->create();
$this->customerResource->load($customerModel, $id);
$customerModel->getEmail();
}
here the first parameter is the model object and the second parameter is id you want to load.
An alternative to Magento 2 Deprecated Save Method is the resource model save method.
public funtion save($taskData)
{
$taskModel = $this->taskFactory->create()->setData($taskData);
$this->resource->save($taskModel);
}
save method accepts only one parameter that is your model object.
- 197,939
- 53
- 422
- 830
- 2,122
- 4
- 25
- 35
See description in the class code https://github.com/magento/magento2/blob/2.1/lib/internal/Magento/Framework/Model/AbstractModel.php#L626
- 197,939
- 53
- 422
- 830
- 4,496
- 20
- 23