Skip to content

Commit 76c3f14

Browse files
committed
MC-36521: SQLSTATE[42S02]: Base table or view not found while placing order
1 parent 2716c26 commit 76c3f14

File tree

3 files changed

+20
-95
lines changed

3 files changed

+20
-95
lines changed

app/code/Magento/Sales/Model/Order.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Config\Model\Config\Source\Nooptreq;
99
use Magento\Directory\Model\Currency;
10+
use Magento\Directory\Model\RegionFactory;
1011
use Magento\Framework\Api\AttributeValueFactory;
1112
use Magento\Framework\Api\SearchCriteriaBuilder;
1213
use Magento\Framework\App\Config\ScopeConfigInterface;
@@ -307,6 +308,11 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
307308
*/
308309
private $scopeConfig;
309310

311+
/**
312+
* @var RegionFactory
313+
*/
314+
private $regionFactory;
315+
310316
/**
311317
* @param \Magento\Framework\Model\Context $context
312318
* @param \Magento\Framework\Registry $registry
@@ -340,6 +346,7 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
340346
* @param OrderItemRepositoryInterface $itemRepository
341347
* @param SearchCriteriaBuilder $searchCriteriaBuilder
342348
* @param ScopeConfigInterface $scopeConfig
349+
* @param RegionFactory $regionFactory
343350
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
344351
*/
345352
public function __construct(
@@ -374,7 +381,8 @@ public function __construct(
374381
ProductOption $productOption = null,
375382
OrderItemRepositoryInterface $itemRepository = null,
376383
SearchCriteriaBuilder $searchCriteriaBuilder = null,
377-
ScopeConfigInterface $scopeConfig = null
384+
ScopeConfigInterface $scopeConfig = null,
385+
RegionFactory $regionFactory = null
378386
) {
379387
$this->_storeManager = $storeManager;
380388
$this->_orderConfig = $orderConfig;
@@ -403,6 +411,7 @@ public function __construct(
403411
$this->searchCriteriaBuilder = $searchCriteriaBuilder ?: ObjectManager::getInstance()
404412
->get(SearchCriteriaBuilder::class);
405413
$this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->get(ScopeConfigInterface::class);
414+
$this->regionFactory = $regionFactory ?: ObjectManager::getInstance()->get(RegionFactory::class);
406415

407416
parent::__construct(
408417
$context,
@@ -1346,9 +1355,14 @@ public function getShippingMethod($asObject = false)
13461355
*/
13471356
public function getAddressesCollection()
13481357
{
1358+
$region = $this->regionFactory->create();
13491359
$collection = $this->_addressCollectionFactory->create()->setOrderFilter($this);
13501360
if ($this->getId()) {
13511361
foreach ($collection as $address) {
1362+
$region->loadByName($address->getRegion(), $address->getCountryId());
1363+
if ($region->getName()) {
1364+
$address->setRegion($region->getName());
1365+
}
13521366
$address->setOrder($this);
13531367
}
13541368
}

app/code/Magento/Sales/Model/ResourceModel/Order/Address/Collection.php

Lines changed: 4 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,8 @@
77

88
use Magento\Sales\Api\Data\OrderAddressSearchResultInterface;
99
use Magento\Sales\Model\ResourceModel\Order\Collection\AbstractCollection;
10-
use Magento\Framework\Locale\ResolverInterface;
11-
use Magento\Framework\Data\Collection\EntityFactoryInterface;
12-
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface;
13-
use Magento\Framework\Event\ManagerInterface;
14-
use Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot;
15-
use Magento\Framework\DB\Adapter\AdapterInterface;
16-
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
17-
use Magento\Framework\App\ObjectManager;
18-
use Psr\Log\LoggerInterface;
10+
use Magento\Sales\Model\Order\Address;
11+
use Magento\Sales\Model\ResourceModel\Order\Address as AddressResource;
1912

2013
/**
2114
* Order addresses collection
@@ -36,44 +29,6 @@ class Collection extends AbstractCollection implements OrderAddressSearchResultI
3629
*/
3730
protected $_eventObject = 'order_address_collection';
3831

39-
/**
40-
* @var ResolverInterface
41-
*/
42-
private $localeResolver;
43-
44-
/**
45-
* @param EntityFactoryInterface $entityFactory
46-
* @param LoggerInterface $logger
47-
* @param FetchStrategyInterface $fetchStrategy
48-
* @param ManagerInterface $eventManager
49-
* @param Snapshot $entitySnapshot
50-
* @param AdapterInterface|null $connection
51-
* @param AbstractDb|null $resource
52-
* @param ResolverInterface|null $localeResolver
53-
*/
54-
public function __construct(
55-
EntityFactoryInterface $entityFactory,
56-
LoggerInterface $logger,
57-
FetchStrategyInterface $fetchStrategy,
58-
ManagerInterface $eventManager,
59-
Snapshot $entitySnapshot,
60-
AdapterInterface $connection = null,
61-
AbstractDb $resource = null,
62-
ResolverInterface $localeResolver = null
63-
) {
64-
$this->localeResolver = $localeResolver ?: ObjectManager::getInstance()
65-
->get(ResolverInterface::class);
66-
parent::__construct(
67-
$entityFactory,
68-
$logger,
69-
$fetchStrategy,
70-
$eventManager,
71-
$entitySnapshot,
72-
$connection,
73-
$resource
74-
);
75-
}
76-
7732
/**
7833
* Model initialization
7934
*
@@ -82,21 +37,11 @@ public function __construct(
8237
protected function _construct()
8338
{
8439
$this->_init(
85-
\Magento\Sales\Model\Order\Address::class,
86-
\Magento\Sales\Model\ResourceModel\Order\Address::class
40+
Address::class,
41+
AddressResource::class
8742
);
8843
}
8944

90-
/**
91-
* @inheritdoc
92-
*/
93-
protected function _initSelect()
94-
{
95-
parent::_initSelect();
96-
$this->joinRegions();
97-
return $this;
98-
}
99-
10045
/**
10146
* Redeclare after load method for dispatch event
10247
*
@@ -110,31 +55,4 @@ protected function _afterLoad()
11055

11156
return $this;
11257
}
113-
114-
/**
115-
* Join region name table with current locale
116-
*
117-
* @return $this
118-
*/
119-
private function joinRegions()
120-
{
121-
$locale = $this->localeResolver->getLocale();
122-
$connection = $this->getConnection();
123-
124-
$defaultNameExpr = $connection->getIfNullSql(
125-
$connection->quoteIdentifier('rct.default_name'),
126-
$connection->quoteIdentifier('main_table.region')
127-
);
128-
$expression = $connection->getIfNullSql($connection->quoteIdentifier('rnt.name'), $defaultNameExpr);
129-
130-
$regionId = $connection->quoteIdentifier('main_table.region_id');
131-
$condition = $connection->quoteInto("rnt.locale=?", $locale);
132-
$rctTable = $this->getTable('directory_country_region');
133-
$rntTable = $this->getTable('directory_country_region_name');
134-
135-
$this->getSelect()
136-
->joinLeft(['rct' => $rctTable], "rct.region_id={$regionId}", [])
137-
->joinLeft(['rnt' => $rntTable], "rnt.region_id={$regionId} AND {$condition}", ['region' => $expression]);
138-
return $this;
139-
}
14058
}

dev/tests/integration/testsuite/Magento/Sales/Model/ResourceModel/Order/Address/CollectionTest.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ class CollectionTest extends TestCase
2929
*/
3030
private $localeResolverMock;
3131

32-
/**
33-
* @var CollectionFactory
34-
*/
35-
private $addressCollectionFactory;
36-
3732
/**
3833
* @inheritdoc
3934
*/
@@ -78,8 +73,6 @@ protected function setUp()
7873
->setStoreId(Bootstrap::getObjectManager()->get(StoreManagerInterface::class)->getStore()->getId())
7974
->setPayment($payment);
8075
$order->save();
81-
82-
$this->addressCollectionFactory = Bootstrap::getObjectManager()->get(CollectionFactory::class);
8376
}
8477

8578
/**
@@ -93,7 +86,7 @@ public function testCollectionWithJpLocale()
9386
$order = Bootstrap::getObjectManager()->create(Order::class)
9487
->loadByIncrementId('100000001');
9588

96-
$collection = $this->addressCollectionFactory->create()->setOrderFilter($order);
89+
$collection = $order->getAddressesCollection();
9790
foreach ($collection as $address) {
9891
$this->assertEquals('アラバマ', $address->getData(OrderAddress::REGION));
9992
}

0 commit comments

Comments
 (0)