2

I have been working on a project where I need to update and save data to a custom attribute to both customers and sales orders. I have created the attributes for both models, and use a helper class for all of my methods. I am using an observer and multiple controllers to call the helper methods, but have been receiving strange results.

Everything works as intended when the methods are executed from the observer, but when the methods are being executed from a controller, I receive a SQL error. I also want to note that this error is only coming from the Order Resource Model when saving an order object, but not the Customer Resource Model when saving a customer object.

Here is some of the helper methods. This portion works as intended

 $this->_customer->setData('[NAMESPACE]_id', $value);
 $this->_customerResourceModel->save($this->_customer);

This portion is not working when called from controller. Works fine when called from observer

 $this->_order->setData('[NAMESPACE]_id', $value);
 $this->_orderResourceModel->save($this->_order);

This is the error that is returned after the method is called from a controller

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'magento2.[NAMESPACE]' doesn't exist, query was: INSERT INTO sales_order_grid ([NAMESPACE]_id) SELECT sales_order.[NAMESPACE]_id AS [NAMESPACE]_id FROM sales_order LEFT JOIN [NAMESPACE] ON sales_order.entity_id = [NAMESPACE].order_id WHERE (sales_order.entity_id = '77') ON DUPLICATE KEY UPDATE [NAMESPACE]_id = VALUES([NAMESPACE]_id)

I should note that I tried adding a [NAMESPACE] table, but that resulted in more SQL errors. The only alteration to the database was to add columns to the customer and sales order tables, and add the attributes.

Any help or point in the right direction is greatly appreciated! Thank you.

Here is the controller where the methods are being called

public function __construct(
    Context $context,
    \[NAMESPACE]\[MODULE]\Helper\Data $helper,
){
    $this->helper = $helper;
    parent::__construct($context);
}

public function execute()
{
    $id = $this->getRequest()->getParam('id');
    $transfer = $this->helper->transferOrder($id);

    $this->_redirect('sales/order/view/order_id/'.$id.'/');
}

Here is the observer where the methods are being called

public function __construct(
    \[NAMESPACE]\[MODULE]\Helper\Data $helper
)
{
    $this->helper = $helper;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $id = $observer->getEvent()->getData()["order_ids"][0];
    $this->helper->transferOrder($id);
}

The $id values in all instances are correct.

  • Can you post your controller code lines here? – Khoa TruongDinh Dec 22 '16 at 16:05
  • @KhoaTruongDinh I have updated the question – StevanZakel Dec 22 '16 at 16:39
  • Can you post your transferOrder method? – Khoa TruongDinh Dec 22 '16 at 16:40
1

Alright, so I figured it out; and its a "face-palm" kind of answer.

The differentiating results were being caused by a poorly setup in my di.xml file.

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.