1

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 Rural\OracleOrderSync\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Model\Order;
use Psr\Log\LoggerInterface;
use Rural\OracleOrderSync\Model\ResponseFactory;
use Rural\OracleOrderSync\Model\Config;

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 Rural\OracleOrderSync\Model;
use Magento\Framework\Stdlib\DateTime;

class Response extends \Magento\Framework\Model\AbstractModel
{
    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('Rural\OracleOrderSync\Model\Resource\Response');
    }

    /**
     * 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;
    }
}
2
  • There may be a typo in the Rural\OracleOrderSync\Model\Resource\Response. Should it be the Rural\OracleOrderSync\Model\ResourceModel\Response (with ResourceModel like Magento does)? Sep 8, 2022 at 14:33
  • The folder name is Resource instead of ResourceModel, that's okay. Sep 12, 2022 at 17:26

1 Answer 1

0

In your model, you are missing the function

protected function _construct()
    {
        $this->_init(ResourceModel\...::class);
    }

So, you will need to create a class that often sits in a folder called ResourceModel and the class may have the same name. This class will be like the below:

class Response extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    /**
     * @inheritDoc
     */
    protected function _construct()
    {
        $this->_init('table_name', 'id'); // table_name should be created in a db_schema.xml file
    }

this https://magemastery.net/courses/magento-2-beginner/model-magento-2 may be helful

1
  • $this->_init(\Rural\OracleOrderSync\Model\Resource\Response::class); is my ResourceModel, function is there. Sep 8, 2022 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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