Skip to content

Magento 2: The resource isn’t set

I’m using Magento 2.4, I created an event & observer checkout_submit_all_after, and after placing an order I get the following error below in the log. Thanks for the help.

LOG

main.ERROR: The resource isn't set.
{"File":"/var/www/html/ruralking.local/vendor/magento/framework/Model/AbstractModel.php",
"Line":479,"Trace":
[{"file":"/var/www/html/ruralking.local/vendor/magento/framework/Model/AbstractModel.php"
,"line":658,"function":"_getResource","class":"Magento\Framework\Model\AbstractModel",
"type":"->"},
{"file":"/var/www/html/ruralking.local/app/code/Rural/OracleOrderSync/Observer/OrderSucce
ss.php","line":75,"function":"save"

Observer/OrderSuccess.php

<?php
namespace RuralOracleOrderSyncObserver;

use MagentoFrameworkEventObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoSalesModelOrder;
use PsrLogLoggerInterface;
use RuralOracleOrderSyncModelResponseFactory;
use RuralOracleOrderSyncModelConfig;

class OrderSuccess implements ObserverInterface
{
    /** @var LoggerInterface  */
    private $logger;
    /**
     * @var ResponseFactory
     */
    private $responseFactory;
    /**
     * @var Config
     */
    private $config;

    /**
     * OrderSuccess constructor.
     * @param LoggerInterface $logger
     * @param Config $config
     * @param ResponseFactory $responseFactory
     */
    public function __construct(
        LoggerInterface $logger,
        Config $config,
        ResponseFactory $responseFactory
    ) {
        $this->logger = $logger;
        $this->config = $config;
        $this->responseFactory = $responseFactory;
    }

    /**
     * @param Observer $observer
     */
    public function execute(Observer $observer): void
    {
        try {
            if (!$this->config->isOracleOrderSyncEnabled()) {
                $this->logger->info("Oracle Order sync module is Disabled");
                return;
            }
            /** @var Order $order */
            $orders = $observer->getData('orders');
            if (empty($orders)) {
                $orders[] = $observer->getData('order');
            }
            foreach ($orders as $order) {
                if (!$order instanceof Order) {
                    continue;
                }
                $order_id = $order->getId();
                $this->logger->info("From Observer: Order id is " . $order_id);
                $response = $this->responseFactory->create();
                $response->addData([
                    "order_id" => $order_id,
                    "confirm_no" => 0,
                    "sync_status" => 0,
                    "sync_retries" => 0,
                    "cutover_order" => $this->cutOverOrder(),
                    "increment_id" => $order->getIncrementId()
                ]);
                $response->save();
            }
        } catch (Exception $e) {
            $this->logger->error(
                $e->getMessage(),
                [
                    'File' => $e->getFile(),
                    'Line' => $e->getLine(),
                    'Trace' => $e->getTrace()
                ]
            );
        }
    }

    /**
     * cutover order flag
     * @return int
     */
    public function cutOverOrder()
    {
        $cutover_order_status = 0;
        if (!$this->config->isPublishOracleMessageEnable()) {
            $cutover_order_status = 1;
        }
        return $cutover_order_status;
    }
}

Model/Response.php

<?php
namespace RuralOracleOrderSyncModel;
use MagentoFrameworkStdlibDateTime;

class Response extends MagentoFrameworkModelAbstractModel
{
    private $dateTime;

    public function __construct(
        DateTime $dateTime,
    ) {
        $this->dateTime = $dateTime;
    }

    /**
     * Cache tag
     *
     * @var string
     */
    const CACHE_TAG = 'oracle_order_create';

    /**
     * Cache tag
     *
     * @var string
     */
    protected $_cacheTag = 'oracle_order_create';

    /**
     * Event prefix
     *
     * @var string
     */
    protected $_eventPrefix = 'oracle_order_create';


    /**
     * Initialize resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('RuralOracleOrderSyncModelResourceResponse');
    }

    /**
     * Get identities
     *
     * @return array
     */
    public function getIdentities()
    {
        return [self::CACHE_TAG . '_' . $this->getId()];
    }

    /**
     * get entity default values
     *
     * @return array
     */
    public function getDefaultValues()
    {
        $values = [];

        return $values;
    }

    public function beforeSave()
    {
        parent::beforeSave();
        $this->setUpdatedAt($this->dateTime->formatDate(true));

        return $this;
    }
}