58

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?

2

4 Answers 4

38

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.

10
  • I see. This makes sense. But can you confirm that all the core CRUD modules will have service contracts at one point?
    – Marius
    Commented May 12, 2016 at 6:10
  • 1
    I 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
    Commented May 12, 2016 at 9:05
  • 2
    yes, for your module better to use ResourceModel in your Module SL
    – KAndy
    Commented May 12, 2016 at 10:39
  • 6
    can you please give some example code for how we can use ResourceModel Commented Nov 8, 2016 at 9:45
  • 1
    Do 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? Commented Nov 15, 2017 at 15:38
25

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

3
6

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.

4

See description in the class code https://github.com/magento/magento2/blob/2.1/lib/internal/Magento/Framework/Model/AbstractModel.php#L626

3
  • 1
    Any idea when load save and delete are going to go away?
    – Marius
    Commented Jun 22, 2017 at 6:02
  • 1
    They will stay for 2-3 minor releases
    – Anton Kril
    Commented Jun 24, 2017 at 19:53
  • and yet here we are. Commented Oct 10, 2022 at 13:55

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.