Prestashop不会增加发票号码

时间:2016-10-10 08:06:06

标签: php mysql prestashop-1.6

我在订单成功页面IF"下一个订单发票编号"不是从管理员手动设置的。

Fatal error: Uncaught You can't specify target table 'ps_order_invoice' for update in FROM clause<br /><br /><pre>UPDATE `ps_order_invoice` SET number =(SELECT new_number FROM (SELECT (MAX(`number`) + 1) AS new_number FROM `ps_order_invoice`) AS result) WHERE `id_order_invoice` = 54</pre> thrown in /classes/db/Db.php on line 791

我已将错误缩小到class / order / Order.php

if ($number) {
    Configuration::updateValue('PS_INVOICE_START_NUMBER', false, false, null, $id_shop);
}

$sql = 'UPDATE `'._DB_PREFIX_.'order_invoice` SET number =';

if ($number) {
    $sql .= (int)$number;
} else {
    $sql .= '(SELECT new_number FROM (SELECT (MAX(`number`) + 1) AS new_number
    FROM `'._DB_PREFIX_.'order_invoice`'.(Configuration::get('PS_INVOICE_RESET') ?
        ' WHERE DATE_FORMAT(`date_add`, "%Y") = '.(int)date('Y') : '').') AS result)';
}

$sql .= ' WHERE `id_order_invoice` = '.(int)$order_invoice_id;

return Db::getInstance()->execute($sql);
}

这是常见问题吗?如何解决这个问题而不对查询进行硬编码? Prestashop版本1.6.1.6

1 个答案:

答案 0 :(得分:0)

谷歌搜索3分钟后,我找到了解决方案。

  1. 从这里MySQL Error 1093 - Can't specify target table for update in FROM clause
  2.   

    在MySQL中,您无法修改在SELECT中使用的同一个表   部分。此行为记录在:   http://dev.mysql.com/doc/refman/5.6/en/update.html

         

    您需要停止使用嵌套子查询并执行   操作分为两部分,或者使用简单的where子句。

    1. 来自Prestashop论坛
    2. https://github.com/PrestaShop/PrestaShop/commit/34ad2f5f45e8b02ce7c3174d94644d6114399746

      if ($number) {
                  Configuration::updateValue('PS_INVOICE_START_NUMBER', false, false, null, $id_shop);
              }
              $sql = 'UPDATE `'._DB_PREFIX_.'order_invoice` SET number =';
              if ($number) {
                  $sql .= (int)$number;
              } else {
                  $getNumberSql = '(SELECT new_number FROM (SELECT (MAX(`number`) + 1) AS new_number
                  FROM `'._DB_PREFIX_.'order_invoice`'.(Configuration::get('PS_INVOICE_RESET') ?
                      ' WHERE DATE_FORMAT(`date_add`, "%Y") = '.(int)date('Y') : '').') AS result)';
                  $getNumberSqlRow = Db::getInstance()->getRow($getNumberSql);
                  $newInvoiceNumber = $getNumberSqlRow['new_number'];
                  $sql .= $newInvoiceNumber;
              }
              $sql .= ' WHERE `id_order_invoice` = '.(int)$order_invoice_id;