1

Need some clarity or correction in my Observer class where I'm trying to save the product when it is updated, but I get "Call to member function dispatch() on null"

Here is my events.xml

<event name="catalog_product_save_after">
    <observer name="catalog_product_save_after_handler" instance="Vendor\Module\Observer\SyncObserver"/>
</event>

Vendor\Module\Observer\SyncObserver.php

<?php
namespace Vendor\Module\Observer;

use \Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class SyncObserver implements ObserverInterface
{
  protected $_queue;

/**
 * @var \Vendor\Module\Model\QueueFactory
 */
protected $syncEngineFactory;

/**
 * @var \Psr\Log\LoggerInterface
 */
protected $logger;

/**
 * @param \Vendor\Module\Model\ResourceModel\Queue $queue
 */
function __construct
(
    \Vendor\Module\Model\ResourceModel\Queue $queue
)
{   
    $this->_queue = $queue;
}

public function execute(EventObserver $observer)
{
    $product = $observer->getEvent()->getProduct();
        $this->productUpdate($product);
        return $this;
}

  public function productUpdate($product){
    //Till here the my product gets saved!!!
  }
}

Now in my productUpdate() has current code custom as:


UPDATED:

public function productUpdate($product)
{
    //$product = $observer->getEvent()->getProduct();   
    $product_id = $product->getId();  //i get the id here

        if(!$product_id) {
            return;
        }
        //Check already record is exists in queue table
        $existingProduct = $this->_queue->load($product_id,'entity_id');   //problem here...

^ Here I get:

Recoverable Error: Argument 1 passed to Magento\Framework\Model\ResourceModel\Db\AbstractDb::load() must be an instance of Magento\Framework\Model\AbstractModel, string given, called in app\code\Vendor\Module\Observer\SyncObserver.php on line 51 and defined in lib\internal\Magento\Framework\Model\ResourceModel\Db\AbstractDb.php on line 333

        $_id = $existingProduct->getId();

        if(empty($_id)) {
            $data = array(
                "product_id" => (int) $product_id
            );

                $this->_queue->setData($data);
                try {
                    $queue_id = $this->_queue->save()->getId();

                    //custom defined log
                    /* $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/productUpdate.log');
                    $logger = new \Zend\Log\Logger();
                    $logger->addWriter($writer);
                    $logger->info("Queueing: [ ".$product_id." ] new product"); */

                } catch(Exception $e) {
                    $this->logger->log(null, "Sync Queue: error adding product [ ".$product_id." ] in queue");

                }

        } else {
            $logger->info("Sync Queue: Error adding product  [".$product_id." ] in queue");
        }
   }

Vendor\Module\Model\ResourceModel\Queue.php

protected function _construct()
{
        $this->_init('tgs_queue','id');
}

What's the correct usage of my load() 1st argument I get the above error.

Cause i'm already calling the $this->_queue->load in Observers constructor.

Please correct me the proper code in the productUpdate() function.

I'm trying to save the product id in my custom table upon changing or updating any product details

2

Change below line in your Vendor\Module\Observer\SyncObserver.php

<?php
namespace Vendor\Module\Observer;

use \Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class SyncObserver implements ObserverInterface
{
  protected $_queue;

/**
 * @var \Vendor\Module\Model\QueueFactory
 */
protected $syncEngineFactory;

/**
 * @var \Psr\Log\LoggerInterface
 */
protected $logger;

/**
 * @param \Vendor\Module\Model\QueueFactory $queue
 */
function __construct
(
    \Vendor\Module\Model\QueueFactory $queue // change here
)
{   
    $this->_queue = $queue;
}

Also, change in productUpdate()

 public function productUpdate($product)
{
    //$product = $observer->getEvent()->getProduct();   
    $product_id = $product->getId();  //i get the id here

        if(!$product_id) {
            return;
        }
        //Check already record is exists in queue table
        $existingProduct = $this->_queue->create()->load($product_id,'entity_id');   //solution here...
  • I get Call to undefined method Vendor\Module\Model\QueueFactory::load() in my Observer – Sushivam Aug 10 '16 at 7:30
  • Is Queue.php model file located in model directory ? – Keyur Shah Aug 10 '16 at 7:31
  • Yes inside Model directory – Sushivam Aug 10 '16 at 7:33
  • Is that Model or ResourceModel..? in \Vendor\Module\Model\QueueFactory... – Sushivam Aug 10 '16 at 7:44
  • It's Model @SachinS – Keyur Shah Aug 10 '16 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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