1

Fatal error: Uncaught Error: Call to undefined method Magento\Framework\App\ResourceConnection::load() in /var/www/html/magento2_dev/vendor/magento/framework/Model/AbstractModel.php:536

Just adding the resource connection on my extended Sales Model, getting the above issue.

Below is my code:

<?php
namespace Sdi\Sales\Model;
use Magento\Framework\App\Request\DataPersistorInterface;
class Order extends \Magento\Sales\Model\Order
{
   protected $_resource;
   public function __construct(
        \Magento\Framework\App\ResourceConnection $resource
) {
    $this->_resource = $resource;
}
public function bnDelete()
{
    if ($this->getStatus() == Mage_Sales_Model_Order::STATE_CANCELED) {
        $connection = $this->_resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
        $query = 'SQL QUERY';
        $connection->query($query);
    }
    return false;
}
}
0

It looks like you are trying to do sql queries from within class. Need to do something like this

public function __construct(
     \Magento\Framework\App\ResourceConnection $resource
)
{
    $this->connection = $resource->getConnection();
    $this->resource = $resource;
}

public function sampleMethod()
{
    $tableName = $this->resource->getTableName('table');
    $select =  $this->connection
       ->select()
        ->from(['o' => $tableName])
        ->where('o.value=?', '2')
        ->group('o.value')
       ->order('o.value', 'ASC');
    echo (string) $select;
    return $select;
}

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.