1

I have the following Observer event that is called after the product is saved.

Subscribers

is a custom model

<?php

namespace Namespace\Modulename\Observer;

use Magento\Framework\Event\ObserverInterface;
use Namespace\Modulename\Model\SubscribersFactory;

class Productsaveafter implements ObserverInterface {

    public function __construct(
    SubscribersFactory $modelSubscribersFactory
    ) {
        $this->_modelSubscribersFactory = $modelSubscribersFactory;
    }

    public function execute(\Magento\Framework\Event\Observer $observer) {
        $_product = $observer->getProduct();  // you will get product object
        $_sku = $_product->getSku(); // for sku
        $subscriber_product_id = $_product->getId();//gives '2'
        $subscriber = $this->_modelSubscribersFactory->create()->getCollection();//This is working

        $subscriber = $this->_modelSubscribersFactory->create()->load(2);//This is not working

    }

}

I can not figure out why ths 'load' function is not working.

Rajeev K Tomy
  • 17,234
  • 6
  • 61
  • 103
Magento Learner
  • 740
  • 9
  • 38

1 Answers1

1

You should not use load() as per Magento Best Practice. Please implement Service Contracts for your Module and use getById($id) method

Yogesh Agarwal
  • 1,162
  • 11
  • 22