Skip to content

Commit

Permalink
Merge pull request #17 from allieshq/mysql-strict
Browse files Browse the repository at this point in the history
Resolve #16 (Insert errors in MySQL strict mode) by casting integers …
  • Loading branch information
odi-um authored Jul 21, 2017
2 parents 05bab22 + 4077f16 commit 5c5819e
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Model/Order/OrderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ public function createOrder(SimpleXMLElement $data)
$paymentMethod = (string) $data->PaymentMethod;
$currencyId = (string) $data->OrderTotal->CurrencyCode;
$totalAmount = (string) $data->OrderTotal->Amount;

// Contingency for strict SQL checks in MySQL 5.7
// See: https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sql-mode-strict

// Decimal
foreach (['totalAmount'] as $k) {
${$k} = ('' === ${$k}) ? null : (float)${$k};
}

// Integer
foreach (['numberOfItemsShipped', 'numberOfItemsUnshipped'] as $k) {
${$k} = ('' === ${$k}) ? null : (int)${$k};
}

$shipping = new Shipping($shipServiceLevel, $shipmentServiceLevelCategory, $numberOfItemsShipped, $numberOfItemsUnshipped);
$payment = new Payment($paymentMethod, $currencyId, $totalAmount);
Expand Down Expand Up @@ -82,6 +95,19 @@ private function processOrderItems($items, Order $order)
$giftWrapPriceAmount = (string) $item->GiftWrapPrice->Amount;
$giftWrapLevel = (string) $item->GiftWrapLevel;
$condition = (string) $item->ConditionId;

// Contingency for strict SQL checks in MySQL 5.7
// See: https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sql-mode-strict

// Decimal
foreach (['itemPriceAmount', 'shippingPriceAmount', 'codFeeAmount', 'codFeeDiscountAmount', 'giftWrapPriceAmount'] as $k) {
${$k} = ('' === ${$k}) ? null : (float)${$k};
}

// Integer
foreach (['quantityOrdered', 'quantityShipped'] as $k) {
${$k} = ('' === ${$k}) ? null : (int)${$k};
}

$itemInfo = new ItemInfo($orderItemId, $title, $quantityOrdered, $quantityShipped,
$itemPriceCurrencyId, $itemPriceAmount, $condition);
Expand Down

0 comments on commit 5c5819e

Please sign in to comment.