【问题标题】:Column exists but throwing Column not found: 1054 Unknown column 'so.event_id' in 'where clause' while inserting into sales_order列存在但抛出 Column not found: 1054 Unknown column 'so.event_id' in 'where clause' 同时插入到 sales_order
【发布时间】:2019-08-16 17:15:09
【问题描述】:

我的 sales_order 表中有 event_id 列。我想下订单,我没有发送 event_id,因为它默认为零。当我尝试下订单时,它向我抛出错误 Column not found: 1054 Unknown column 'so.event_id' in 'where Clause',查询是:INSERT INTO sales_order ...

这是 INSERT 查询中出现的错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'event_id' in 'where clause', query was: INSERT INTO `sales_order` (`state`, `status`, `protect_code`, `shipping_description`, `is_virtual`, `store_id`, `customer_id`, `base_discount_amount`, `base_grand_total`, `base_shipping_amount`, `base_shipping_tax_amount`, `base_subtotal`, `base_tax_amount`, `base_to_global_rate`, `base_to_order_rate`, `discount_amount`, `grand_total`, `shipping_amount`, `shipping_tax_amount`, `store_to_base_rate`, `store_to_order_rate`, `subtotal`, `tax_amount`, `total_qty_ordered`, `customer_is_guest`, `customer_note_notify`, `customer_group_id`, `quote_id`, `base_shipping_discount_amount`, `base_subtotal_incl_tax`, `base_total_due`, `shipping_discount_amount`, `subtotal_incl_tax`, `total_due`, `weight`, `increment_id`, `applied_rule_ids`, `base_currency_code`, `customer_email`, `customer_firstname`, `customer_lastname`, `customer_middlename`, `discount_description`, `global_currency_code`, `order_currency_code`, `shipping_method`, `store_currency_code`, `store_name`, `total_item_count`, `discount_tax_compensation_amount`, `base_discount_tax_compensation_amount`, `shipping_discount_tax_compensation_amount`, `base_shipping_discount_tax_compensation_amnt`, `shipping_incl_tax`, `base_shipping_incl_tax`, `gift_message_id`, `has_ambassador`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

我已经多次运行所有缓存和升级命令,但我不知道为什么要这样做。 这是我的代码:

public function createMageOrder($orderData) {
        $store = $this->_storeManager->getStore();
        $websiteId = $this->_storeManager->getStore()->getWebsiteId();
        $customer = $this->customerFactory->create();
        $customer->setWebsiteId($websiteId);
        $customer->loadByEmail($orderData["entity"]["customer_email"]); // load customet by email address
        if (!$customer->getEntityId()) {
            echo "Customer does not exist. please create here...";
            exit;
            //If not avilable then create this customer 
            $customer->setWebsiteId($websiteId)
                    ->setStore($store)
                    ->setFirstname($orderData['shipping_address']['firstname'])
                    ->setLastname($orderData['shipping_address']['lastname'])
                    ->setEmail($orderData['email'])
                    ->setPassword($orderData['email']);
            $customer->save();
        }
        $quote = $this->quote->create(); //Create object of quote
        $quote->setStore($store); //set store for which you create quote
        // if you have allready buyer id then you can load customer directly 
        $customer = $this->customerRepository->getById($customer->getEntityId());
        $quote->setCurrency();
        $quote->assignCustomer($customer); //Assign quote to customer
        //add items in quote
        foreach ($orderData["entity"]["items"] as $item) {
            $product = $this->_product->load($item['product_id']);
            $product->setPrice($item['price']);
            $quote->addProduct(
                    $product, intval($item['qty_ordered'])
            );
        }

        //Set Address to quote
        $quote->getBillingAddress()->addData($orderData["entity"]["billing_address"]);
        $quote->getShippingAddress()->addData($orderData["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]);

        // Collect Rates and Set Shipping & Payment Method
        $shippingAddress = $quote->getShippingAddress();
        $shippingAddress->setCollectShippingRates(true)
                ->collectShippingRates()
                ->setShippingMethod($orderData["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["method"]); //shipping method
        $quote->setPaymentMethod($orderData["entity"]["payment"]["method"]); //payment method
        $quote->setInventoryProcessed(false); //not effetc inventory
        $quote->save(); //Now Save quote and your quote is ready
        // Set Sales Order Payment
        $quote->getPayment()->importData(['method' => $orderData["entity"]["payment"]["method"]]);
//        $quote->setData('event_id', $orderData["entity"]["event_id"]);
        // Collect Totals & Save Quote
        $quote->collectTotals()->save();

        // Create Order From Quote
        $order = $this->quoteManagement->submit($quote);

        $order->setEmailSent(0);
        $increment_id = $order->getRealOrderId();
        if ($order->getEntityId()) {
            $result['order_id'] = $order->getRealOrderId();
        } else {
            $result = ['error' => 1, 'msg' => 'Your custom message'];
        }
        return $result;
    }

我在 placeGTOrder($post) 函数中调用此函数,其中 $post 是我的 $orderData。这是将我的请求作为操作发送的函数:

public function placeGTOrder($post) {
        $formData = $this->_helper->unserializeForm($post['formData']);
        $order = array();

        //fill cart items to order items 
        $cart = $this->_objectManager->get('\Magento\Checkout\Model\Cart');
        $itemsCollection = $cart->getQuote()->getItemsCollection();
        $itemsVisible = $cart->getQuote()->getAllVisibleItems();
        $items = $cart->getQuote()->getAllItems();
        $getItems = [];
        foreach ($items as $item) {
            $collect = array();
            $collect["base_original_price"] = 45;
            $collect["base_price"] = 45;
            $collect["base_price_incl_tax"] = 45;
            $collect["base_row_invoiced"] = 0;
            $collect["base_row_total"] = 45;
            $collect["base_tax_amount"] = 0;
            $collect["base_tax_invoiced"] = 0;
            $collect["discount_amount"] = 4.5;
            $collect["discount_percent"] = 10;
            $collect["free_shipping"] = 0;
            $collect["is_virtual"] = 0;
            $collect["name"] = $item->getName();
            $collect["original_price"] = 45;
            $collect["price"] = 45;
            $collect["price_incl_tax"] = 45;
            $collect["product_id"] = $item->getProductId();
            $collect["product_type"] = "simple";
            $collect["qty_ordered"] = $item->getQty();
            $collect["row_total"] = 45;
            $collect["row_total_incl_tax"] = 45;
            $collect["sku"] = $item->getSku();
            $collect["store_id"] = 1;
            $getItems[] = $collect;
        }


//        $order["entity"]["event_id"] = $formData["eventID"];
        $order["entity"]["base_currency_code"] = "USD";
        $order["entity"]["base_discount_amount"] = -4.5;
        $order["entity"]["base_grand_total"] = 45.5;
        $order["entity"]["base_shipping_amount"] = 5;
        $order["entity"]["base_subtotal"] = 45;
        $order["entity"]["base_tax_amount"] = 0;
        $order["entity"]["customer_email"] = urldecode($formData['Email']);
        $order["entity"]["customer_firstname"] = $formData['FirstName'];
        $order["entity"]["customer_group_id"] = 3;
        $order["entity"]["customer_id"] = $formData['selected_customer_id'];
        $order["entity"]["customer_is_guest"] = 0;
        $order["entity"]["customer_lastname"] = $formData['LastName'];
        $order["entity"]["customer_note_notify"] = 1;
        $order["entity"]["discount_amount"] = -4.5;
        $order["entity"]["email_sent"] = 1;
        $order["entity"]["coupon_code"] = "";
        $order["entity"]["discount_description"] = "Test1";
        $order["entity"]["grand_total"] = 45.5;
        $order["entity"]["is_virtual"] = 0;
        $order["entity"]["order_currency_code"] = "USD";
        $order["entity"]["shipping_amount"] = 5;
        $order["entity"]["shipping_description"] = "Flat Rate - Fixed";
        $order["entity"]["state"] = "new";
        $order["entity"]["status"] = "pending";
        $order["entity"]["store_currency_code"] = "USD";
        $order["entity"]["store_id"] = 1;
        $order["entity"]["store_name"] = "Main Website\nMain Website Store\n";
        $order["entity"]["subtotal"] = 45;
        $order["entity"]["subtotal_incl_tax"] = 45;
        $order["entity"]["tax_amount"] = 0;
        $order["entity"]["total_item_count"] = 1;
        $order["entity"]["total_qty_ordered"] = 1;
        $order["entity"]["weight"] = 1;
        $order["entity"]["items"] = $getItems;
        $order["entity"]["billing_address"]["address_type"] = "billing";
        $order["entity"]["billing_address"]["city"] = urldecode($formData['MainAddress.City']);
        $order["entity"]["billing_address"]["company"] = "";
        $order["entity"]["billing_address"]["country_id"] = urldecode($formData["MainAddress.Country"]);
        $order["entity"]["billing_address"]["email"] = urldecode($formData['Email']);
        $order["entity"]["billing_address"]["firstname"] = urldecode($formData['FirstName']);
        $order["entity"]["billing_address"]["lastname"] = urldecode($formData['LastName']);
        $order["entity"]["billing_address"]["postcode"] = urldecode($formData['MainAddress.Zip']);
        $order["entity"]["billing_address"]["region"] = urldecode($formData['region']);
        $order["entity"]["billing_address"]["region_code"] = urldecode($formData['region']);
        $order["entity"]["billing_address"]["region_id"] = 19;
        $order["entity"]["billing_address"]["street"] = array(urldecode($formData["MainAddress.Address1"]), urldecode($formData["MainAddress.Address2"]));
        $order["entity"]["billing_address"]["telephone"] = urldecode($formData["PrimaryPhone"]);
        $order["entity"]["payment"]["method"] = "checkmo";
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["address_type"] = "shipping";
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["city"] = urldecode($formData["MailingAddress.City"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["company"] = "";
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["country_id"] = urldecode($formData["MailingAddress.Country"]);
//        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["customer_address_id"] = 2;
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["email"] = urldecode($formData["Email"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["firstname"] = urldecode($formData["FirstName"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["lastname"] = urldecode($formData["LastName"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["postcode"] = urldecode($formData["MainAddress.Zip"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["region"] = urldecode($formData["region"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["region_code"] = urldecode($formData["region"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["region_id"] = 19;
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["street"] = array(urldecode($formData["MailingAddress.Address1"]), urldecode($formData["MailingAddress.Address2"]));
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["telephone"] = urldecode($formData["PrimaryPhone"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["method"] = "flatrate_flatrate";

        $result = $this->createMageOrder($order);
        echo json_encode($result); exit; 
//        $ApiResponse = $this->_helper->placeOrderByAPI($order);
//        print_r($ApiResponse);
        exit;
        exit;
    }

这是表格:

【问题讨论】:

  • 请检查您的sales_order 表以查看是否确实存在 event_id 列。如果没有,您的 UpgradeSchema 脚本可能不起作用,您需要创建一个 InstallSchema 或 UpgradeSchema 来安装该列。
  • 已经有 installData & Recurring files to upgradeschema in db。 sales_order 表中也存在 event_id 列。
  • 你能给我们看一张你的sales_order表的截图吗
  • 我已经更新了我的问题,请查看最后一张图片。 event_id 已经存在于表中
  • 我很困惑我没有插入事件 id 那么为什么要考虑它呢?通常插入查询只是插入记录而不检查所有列。

标签: php mysql magento magento2


【解决方案1】:

您的查询是完美的,您只需要检查数据库中的触发器,如果​​有任何触发器正在运行,则禁用它或将其删除,然后再次运行您的代码。我希望这会奏效。

这里是查询:

SHOW triggers;

【讨论】:

  • 天啊!我刚刚运行了节目触发器;查询并且有 4 个触发器在订单插入之前触发。这就是为什么他们正在制造问题并且不允许我以编程方式插入订单。这真的很适用。非常感谢。为什么我没有像你一样开箱即用。感谢您的帮助和努力。
猜你喜欢
  • 1970-01-01
  • 2021-12-31
  • 2018-09-10
  • 2020-10-13
  • 1970-01-01
  • 2011-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多